[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How to stop yielded threads from being garbage collected
- From: HyperHacker <hyperhacker@...>
- Date: Wed, 23 Nov 2011 16:19:27 -0700
On Wed, Nov 23, 2011 at 16:16, Martin Linklater <email@example.com> wrote:
> Thanks. So after creating the new thread, stack position -1 contains a thread object. When you say to keep a reference to it do I need to create a Lua table entry which contains that thread object ? Or is there a simpler way ?
> On 23 Nov 2011, at 23:10, HyperHacker wrote:
>> On Wed, Nov 23, 2011 at 15:48, Martin Linklater <firstname.lastname@example.org> wrote:
>>> Hi - apologies for my ignorance, but I'm quite new to Lua.
>>> I am writing a system where multiple Lua threads are created and updated in
>>> series on one CPU thread. The lua code can call a Sleep(n) function which
>>> calls a C function which yields the thread (lua_yield()) and puts it on an
>>> inactive list. This inactive list is checked 30 times a second and any
>>> threads that have slept for long enough are then continued by calling
>>> lua_resume() on them.
>>> This is working fine if I only have a few threads executing, but if I put
>>> the code under stress and create a lot of lua threads, I get the following
>>> error when I call lua_resume on a yielded thread:
>>> cannot resume non-suspended coroutine
>>> After doing a bit of digging around on the internet I think this is being
>>> caused by the thread being garbage collected while it is yielded. Can
>>> someone please verify if my observed behaviour fits in with this diagnosis,
>>> and if so, what is the easiest way to stop a yielded thread from being
>>> garbage collected ?
>>> Thanks for any help you can give.
>>> - Martin
>> It shouldn't be collected as long as there's a reference to it.
>> Sent from my toaster.
I believe so. At least once you return from that function, the
reference on the stack is gone. I'm not sure if it can be collected
while it's still on the stack, but it's easy to accidentally remove it
from there too. Your best bet is to keep them all in a table. The
registry functions might be more convenient than keeping a table on
the stack for this purpose.
Sent from my toaster.