lua-users home
lua-l archive

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


Le 22/04/2016 14:08, Viacheslav Usov a écrit :
On Fri, Apr 22, 2016 at 12:31 PM, Laurent FAILLIE <l_faillie@yahoo.com> wrote:

I read this page but unfortunately, it only explains how to lock and mutex b/w thread, not how to share a function reference w/o locking.

The page explains how you could use one Lua environment from multiple threads. It does need a mutex to protect its internal structures, but that does not make you "rely on main thread activities". With this approach, both of your threads can call lua_pcall with the same Lua environment concurrently, meaning that callbacks can run concurrently with the main thread.
Not concurrently as this page explains : "The method above for implementing threading in Lua using a global mutex is inefficient on multiprocessor systems. As one thread holds the global mutex, other threads are waiting for it. Thus only one Lua thread may be running at a time, regardless of the number of processors in the system."
So, if I have a long running thread, it will block others to run as well.

The only way is to have a dedicated state per thread ... but my problem is to pass function reference to them.