my_application deadlocks when closing with ThreadPool threads running #625
Replies: 3 comments 5 replies
-
Actually. had to do tricks with thread pool, because thread pool joins on destuction. Funny thing was that I hope this research helps anyone. :-) |
Beta Was this translation helpful? Give feedback.
-
I guess the reason it deadlocks is because the thread are calling blocking_invoke_from_event_loop, but the event loop is already exited, so your worker thread are going to block forever. This is a bit tricky to get right as you will ahve to do something like this (pseudo-code) // ...
ui->run();
// this function exit when the window is closed, but you probably don't want that, you want to keep the
// event loop running until all the worker threads are finished.
// send a message somehow to all the worker thread so they stop their work.
pool.tell_the_threads_to_stop();
while (pool.still_working()) {
sixtyfps::run_event_loop()
}
// ...
// ...
// in the worker thread, your code must be like this:
for( /* that's your busy loop */ ) {
// .. do_some_small_work
sixtyfps::blocking_invoke_from_event_loop([&]{
// update the progress
});
std::unique_lock lock(pool.mutex);
if (pool.must_stop()) {
// tell the event loop to stop (NOT BLOCKING)
sixtyfps::invoke_from_event_loop([&]{
sixtyfps::quit_event_loop();
}
pool.notify_thread_finished(this);
return; // this should exist the working thread
}
}
pool.notify_thread_finished(this); So the idea is to make sure that the event loop runs long enough so that no worker thread can be still calling blocked_invoke_from_event_loop. And you must be carefull to signal the event loop to close when all threads are finished. I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Can you explain once more why I should add
in worker? What I can't get is... When I close, event loop stops anyway. So worker threads will probably sleep on blocking. Does event loop run in main or separate thread? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm continuing to learn sixty fps with multiple workers example.
I've used thread pool to run worker threads.
(Actually, I tried two implementations from github.)
And it turns out application deadlocks (my_application.exe is seen in task manager) on close button pressed when I use
I've managed to fix this using std::terminate
But I'm not sure if it is right thing to do.
I'll attach code if you request.
Beta Was this translation helpful? Give feedback.
All reactions