-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Deadlock if wait_for_tasks() is called concurrently #110
Comments
Nice catch, thanks for posting this issue! I believe this happens if the second The ideal solution is to add an additional mutex to ensure that task_done_cv.wait(tasks_lock, [this] { return (tasks_total == (paused ? tasks.size() : 0)); }); with task_done_cv.wait(tasks_lock, [this] { return (tasks_total == (paused ? tasks.size() : 0)) || !waiting; }); and similarly in the other two wait functions if desired. |
Note: I added an extra variable to the example code to make the problem appear even faster.
That does not seem to have an effect. |
Wait, what? Both threads set waiting to This actually brings up another possibility: If thread 2 entered |
FYI, atomics have been specifically built to make test-and-set type operations, well, atomic. So something like One of these would be more appropriate. I'd recommend the strong variants for this project. Though in this case |
You're 100% right, I appear to have made a mistake and
I tested this with your minimal working example and did not encounter any deadlocks (I let it count to 30,000 and then stopped it manually). I also have a test in This fix will be posted within a few days with release v3.5.0 (along with a few other fixes I've been working on). Thanks again for pointing this out, it was very helpful! :) |
Great!
I think that's correct. Notice that now the reproducing code always deadlocks on the first iteration. That's what I expected it to do when I wrote the code, I think the fact it took multiple iterations with stock 3.4.0 is that the second thread would simply not wait.
Yes, this fixes it! |
Update: This issue has been fixed in v3.5.0, which was just released. Thanks again! |
Describe the bug
Deadlock if
wait_for_tasks()
is called from multiple threads at the same time.Minimal working example
Behavior
Expectation is that both
wait_for_tasks()
will block until the tasks are completed then both will unblock. In some situations this does not happen, deadlocking one of the threads.Example execution of above:
Edit: Added
thread_started
to example code to make the problem appear even faster.The text was updated successfully, but these errors were encountered: