[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: occasional threading in lua
- From: "Russ Cox" <rsc@...>
- Date: Fri, 17 Nov 2006 07:14:05 -0500
> I like Lua's coroutine model a lot.
> But sometimes you just need to run on multiple CPUs.
> I couldn't find any interfaces that were very satisfying.
> In particular, I don't want to give up the simplicity of
> cooperative scheduling. I'd like to be able to have certain
> C calls run in parallel with the VM, but otherwise keep
> the VM single-threaded. So I can offload I/O and computation
> into these parallel C calls but keep the usual Lua model.
check my Helper Threads Toolkit (http://luaforge.net/forum/?group_id=163).
it's a general framework that lets you write C modules that run in a separate
pthread.
Thanks. I did look at that, but that's a lot more work for the
programmer each time he wants to wrap a call, no?
One interesting benefit of the module I posted is that if it were
to export vm_lock and vm_unlock, you could even call them
inside Lua to wrap existing modules that didn't anticipate the
framework at all.
For example:
thread.vm_unlock();
line = f:read(*l)
thread.vm_lock();
would run f:read "asynchronously" (letting other coroutines run
while it executes) even though the file routines are not written
expecting to be run that way, and the rest of the program would
remain single-threaded. (The flexibility isn't free: doing this requires
implementing lua_lock and lua_unlock, which I previously didn't have to,
so you can either have this functionality or work with standard
interpreters. But it seems like a reasonable tradeoff.)
Russ