[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua State Thread Safety
- From: Tim Hill <drtimhill@...>
- Date: Mon, 14 Jul 2014 19:07:03 -0700
On Jul 13, 2014, at 11:56 PM, Austin Einter <firstname.lastname@example.org> wrote:
> Hi Tim
> My worry is, with use of our own locks to protect Lua state , will garbage collection pose any thread safety issues.
> Say threads are T1 and T2
> Using lock, I can control T1 and T2, so that only one thread access Lua state at a time
> But garbage collection, in which thread context it executes
> Can it happen that, under T1 I am doing something, and with T2 garbage collection is being executed by system......, hence we will not be able to protect in this case as garbage collection code is not in my control.
> I have not see any issue, just want to make sure I am not doing wrong in my design.
No, this is all safe. Each (primary) Lua state has an independent garbage collector, and so it inherits the same thread-safety as other Lua code as long as you make sure only one thread calls into a given state at a time (via whatever locking/mutex you wish to use). There is no hidden GC thread in Lua; it just runs on the same thread that you use when you call into the Lua API.