lua-users home
lua-l archive

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


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
> wait.   
> That is, using the C API, I do the equivalent of
> 	TableInRegistry[thread]=WaitTimeLeft;
> 
> 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
TableInRegistry[thread]=nil
> *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
> right?)  
> 

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
weak." [1]

So I think your 'TableInRegistry' should be declared as 'weak
table'. I would do some more reading on that topic...

-- Vijay

[1] http://www.3plus4.de/wp-content/lua-51-reference.pdf