-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Prevent Premature Thread Exiting #61
Conversation
Hello! 👋 Thanks for the PR
This is not true unless the thread pool is being destructed. In normal situations, the threads that don't have work just wait for new work to come into the queue. Is this the situation you're trying to address? Where the Additionally, the unit test that was added is tripping address sanitizer:
|
Yes, that is almost the situation; sorry, I forgot to mention that the Here's a breakdown of the scenario:
The test I added is simulating this situation.
I don't see how we can reuse the
Thanks, I fixed it in new commit. |
Ok thanks for the clarification and fix for the unit test!
Maybe I'm missing something, but the main difference between I'm going to play around with you PR and see if I can make that happen. If so, your PR may also work nicely as the base for #23 |
You are right; we can use just the Consider this scenario: all threads are awake, the queues are empty, but one thread is finishing a long-running task. The That is why I chose two counters, but looking back, it does not seem like an elegant solution.
Sounds great, thanks! |
8814340
into
DeveloperPaul123:master
Welp I accidentally pushed this to my master 🤦♂️ But I intended to merge it anyway. I renamed the counter variables and added a new subcase for the unit test you added. Thanks again for the PR! |
You are welcome. I agree that the names for the counter variables are more precise now. Thank you for merging my contribution! |
Thank you for your PR! |
Hi Paul,
Firstly, thank you for the well-written C++ code; thread-pool is an excellent project!
I identified a potential issue where threads in the thread pool might exit prematurely under certain conditions, impacting performance and correctness.
The Scenario:
See the added test case for a simple example.
Proposed Fix:
This merge request introduces an atomic counter to track the total number of tasks. The main thread sleeps on a binary semaphore, which will get signaled by the last thread if there are no other tasks in the thread pool. This ensures all tasks are completed before the main thread signals that all threads should exit.