[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: getfd/setfd for io file objects?
- From: Duck <duck@...>
- Date: Thu, 3 Apr 2008 08:06:21 +1100 (EST)
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?