-
Notifications
You must be signed in to change notification settings - Fork 22
Description
First of all again thanks for all your work on this. My aim is to help you make it better.
There is some design issues with llthreads (going back to the original) when fit into the Lua model.
First of all a minor thing, there's IMHO little reason to have a "detach" carry over pthread concept.
Why not just "thread:start()"? If you don't care when thread completes it should be the same, just don't need to call "thread:join()" unless you really want to wait for it to be done.
In either case the thread will just exit and "thread" will be around until you don't need it any more (then normally garbage collected).
Now the bigger issue, consider this little test case:
local thread = llthreads.new("while true do sleep(1) end")
thread:start()
thread = nil
collectgarbage()
You have to ask yourself "What should happen here?"
IMHO ideally in the Lua paradigm the thread would terminate itself (wherever it's at in the script) and then clean up nicely like any other Lua variable (be it a number, table, etc).
This parallels what happens if you run your "test_alive.lua" test.
As I see it now it will cause a hang in garbage collector for that child thread on exit.
The problem has always been there, but you have to catch it with a debugger to see it.
Also It might just hang your lua.exe on the "lua_close(L);" (for main Lua state).
A solution might be to terminate thread (the handle must be there at the end without the "detach" flag nonsense). But then this a problem maybe, can you just terminate a lua pcall() wherever it's at and then call lua_close() it for final clean up? This would IMHO be ideal.
If someone needs to keep a looping script thread like this this they could probably use one of the sharing libs like ZeroMQ and sync up a shutdown path.
At any rate what I suggest, and what I will add to my local version is to simply catch on garbage collection if a thread is still running. This way at least the user will know "Hey you are not doing right!". The user will be aware that there is a problem. Their lua.exe will not just sit there hung up with a clue to why it's like that.
Tests like "test_alive.lua" will have to be fixed so the script loop will only loop for a max count.
Then a "thread:join()" or a looping "thread:alive()" needs to be there to make sure the script thread has actually terminated.
I will post more in this issue once I've coded my "error on thread still running" solution.
P.S. Ты хорошо знаешь английский?