lua-users home
lua-l archive

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


On Wednesday 02 July 2008, GaoXianchao wrote:
> Hi all,I have a question on luathread:
> Can I share a table between two luathread?
> If I set the table to nil, Dose another thread can continue  to use the
> table ?

i guess you mean something like this:

a={...some data...}
sendToOtherThread(a)
a=nil

in this case, you're not 'setting the table to nil', just setting variable 'a' 
(that used to refer to a table) to nil.  if there's other reference to the 
table, it won't be collected.

another interpretation of you question:

common_table={}

-- in thread A:
common_table[x] = {..some data..}

-- in thread B:
bigslowjob (common_table[x])

-- back in thread A (before bigslowjob() finishes)
common_table[x] = nil

no problem here either, because the parameter in the stack is another 
reference to the table, and won't be collected until (at least) bigslowjob() 
returns.

one more possibility:

common_table={}

-- in thread A:
common_table[x] = {..some data..}

-- in thread B:
k=next (common_table)   -- gets first key in table, lets say k==x
workalongtimeonsomethingelse()
t=common_table[k]        -- gets value associated with k

-- but, in thread A, before workalongtimeonsomethingelse() returns...
common_table[x] = nil

in this case, thread B is assuming the key 'k' stays valid for some time, but 
it might not.

in short: in LuaThreads there's a single LuaState, and a single garbage 
collector.  an object won't be collected as long as there's at least one 
reference to it, no matter where it is.

-- 
Javier

Attachment: signature.asc
Description: This is a digitally signed message part.