lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


于 2011-8-29 23:21, Mateusz Czaplinski 写道:
On Sat, Aug 27, 2011 at 11:34 AM, Marc Balmer<marc@msys.ch>  wrote:
Am 27.08.11 11:29, schrieb steve donovan:
On Sat, Aug 27, 2011 at 11:22 AM, Marc Balmer<marc@msys.ch>  wrote:
How difficult would it be to make Lua true multithreaded?  I know that
is against the intentions of the inventors, but have experiments been
done in this direction?
There is Diego Nehab's LuaThread
Thank you!
Sorry if I'm missing something from the discussion, but maybe the wiki
page with a list of various libraries could be helpful:
   http://lua-users.org/wiki/MultiTasking

regards
/Mateusz Czapliński.

besides this wiki page, I would recommend some excellent blog entries, which contained some great idea on this subject.
this blog is written in Chinese, I would give some brief description of the entries below.
if you can read Chinese, you may read them your self.
:)

1. http://blog.codingnow.com/2011/08/lua_52_multithreaded.html
this entry discussed some idea of `preemptive' multitasking solutions with Lua 5.2.
in fact this is not true preemptive way, but means to transparently yield a lua coroutine periodically.
the main idea here is to set a debug hook that yields transparently, as Lua 5.2 now supports
yielding from a debug hook in C,

one suggested way to implement this is to implement the scheduler in C, while running many seperate lua states, each state
with just the main thread that may yield periodically using some kind of debug hook.
noted that the debug hook may be set asynchronously (see the source code of lua_sethook), this model can be simply extended
so that different lua states can run in multiple `real threads' of the underlying OS.
this is actually a very flexible M:N threading model.

but the author has not give the code, I did some experiment on the beta version of Lua 5.2 myself, and it seems to work OK.

besides, this idea can be further extended.
e.g., I have tried to use SIGALRM to set debug hook.
this emulates a timer interrupt event for the lua VM.
then the debug hook would yield to a scheduler, who picks another thread and resumes it.

when I looked into the lua VM code, I found it would call `traceexec' if certain debug hook are enabled,
just before each OPCODE is executed. this is just similar to a real CPU which check for interrup event every cycle.
and the lua_sethook can be called asynchronously!!!

how do you think it?


2. http://blog.codingnow.com/2010/12/lua_cothread.html
this blog entry introduced a multitasking library/model developed by the author called `cothread',
which is inspired by the `goroutine' of the Go language(http://golang.org).

it is cooperative based on lua coroutine and consists a simple set of API, and a simple scheduler,
to assist writing thread function, and to assist communication.

if you don't read Chinese, you may see to the code metioned in this entry here:
http://blog.codingnow.com/cloud/LuaCothread


hope this may help.