[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: RE: lua_newthread(), good way to keep the thread values?
- From: "Vijay Aswadhati" <vijay@...>
- Date: Wed, 22 Aug 2007 13:40:48 -0700
On Wednesday, August 22, 2007 9:45 AM, Carsten Fuchs wrote:
> Thus, my first attempt was to make a new table in the registry,
> and the put the thread as a key(!) into that table. As the value
> for that key I would use the number of seconds remaining to
> That is, using the C API, I do the equivalent of
> This is nice and simple, e.g. I can easily iterate over the
> TableInRegistry key/value pairs and check for and update the
> WaitTimeLeft and resume the thread when it dropped to zero.
> The problem is, once a thread returned normally (body function
> completed without yield()), I'd do
> *but* setting nil here still won't ever garbage collect the
> thread, does it?
> Well... I was wondering how to best solve the problem. (And if
> my assumption above that the thread is never collected is
If my understanding of Lua is right then I think what you should be
reading about is 'weak table'.
"A weak table can have weak keys, weak values, or both. A table with
weak keys allows the collection of its keys,
but prevents the collection of its values. A table with both weak
keys and weak values allows the collection of
both keys and values. In any case, if either the key or the value is
collected, the whole pair is removed from the
table. The weakness of a table is controlled by the __mode field of
its metatable. If the __mode field is a string
containing the character 'k', the keys in the table are weak. If
__mode contains 'v', the values in the table are
So I think your 'TableInRegistry' should be declared as 'weak
table'. I would do some more reading on that topic...