Prevent Premature Thread Exiting#61
Prevent Premature Thread Exiting#61ptsouchlos merged 3 commits intoptsouchlos:masterfrom GabTux:master
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! |
|
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.