lua-users home
lua-l archive

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


G'day folks.

When using a message-passing multithreading library like LuaTask, where 
each thread has its own Lua state, you pass objects between tasks by 
serialising into a string, passing the string and deserialising...

Diego explained how you can effectively "serialise" a LuaSocket, even 
though it's a userdatum, passing only an fd (int) between the threads:

1. extract socket descriptor using socket:getfd() and pass it

2. create a dud "clone" socket object in the passed-to thread

3. receive the descriptor and graft into the "clone" using socket:setfd()

(Of course, you need to setfd() the original socket object to -1, which is 
not-a-descriptor, so that when it gets collected, the cloned socket's 
descriptor doesn't get closed!)

This works nicely.

Is there a way to do this with the io library's file objects? To pass only 
the fileno() of an io.open()ed file?