Code: Select all
io.stdout:setvbuf('no')
function love.threaderror(thread, errorStr)
error(errorStr)
end
local thread = love.thread.newThread([[
local channel = love.thread.getChannel('channel')
channel.supply('hello')
]])
local channel = love.thread.getChannel('channel')
thread:start()
print(channel:demand())
When you run this program, nothing happens. It doesn't print hello, and it doesn't show any errors. It also becomes completely non-responsive. The task must be killed in order to quit.
I believe this is because the main thread reaches channel:demand() before the thread encounters the error. The main thread is now blocked, waiting for data on the channel; the thread can't supply data because of the error; love.threaderror doesn't get called because the main thread is blocked. Total deadlock. Removing the last line makes the error appear, confirming my theory. Sleeping for a bit before the demand() also makes it work as expected.
The demand() call directly after starting the thread is crucial to my design. I need the worker thread to send a list of identifiers that the main thread requires to continue.
A completely frozen app in case of a thread error is pretty unfortunate. I'd appreciate some ideas/advice on how to solve this problem in a smart way, without sleeps and without resorting to busy polling of the channel.