bshoshany / thread-pool

BS::thread_pool: a fast, lightweight, modern, and easy-to-use C++17 / C++20 / C++23 thread pool library

Repository from Github https://github.combshoshany/thread-poolRepository from Github https://github.combshoshany/thread-pool

Is the pool itself thread-safe?

asceenl opened this issue · comments

commented

Dear Barak Shoshany,

First of all, thank you for sharing this wonderful library. I have a small question, on which by inspecting the code, my answer seems no. The question is: is the pool itself thread-safe? I.e., am I allowed to do pool.submit() from different threads at the same time?

If not, can you please share me an example of how I can type a tiny bit of code to protect a globally instantiated BS::thread_pool?

Thank you in advance for your answer.

Best regards,

Anne de Jong

Good question! Yes, the thread pool itself is thread-safe. Every member function of BS::thread_pool that accesses any data uses a lock/mutex to synchronize that access. Just make sure not to wait for threads from within other threads, as that may cause the pool to wait forever.