Skip to content

Commit 6ba7e87

Browse files
committed
improved arguments check
When an equeue happens during the pool destruction, the user did something terribly wrong! Zero threads are possible but result in an useless pool. There aren't plans for dummy pools, right?
1 parent 3393a05 commit 6ba7e87

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

ThreadPool.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class ThreadPool {
1717
// the constructor just launches some amount of workers
1818
ThreadPool(size_t threads_n = std::thread::hardware_concurrency()) : stop(false)
1919
{
20+
if(!threads_n)
21+
throw std::invalid_argument("more than zero threads expected");
22+
2023
this->workers.reserve(threads_n);
2124
for(; threads_n; --threads_n)
2225
this->workers.emplace_back(
@@ -45,10 +48,6 @@ class ThreadPool {
4548
template<class F, class... Args>
4649
std::future<typename std::result_of<F(Args...)>::type> enqueue(F&& f, Args&&... args)
4750
{
48-
// don't allow enqueueing after stopping the pool
49-
if(this->stop)
50-
throw std::runtime_error("enqueue on stopped ThreadPool");
51-
5251
using packaged_task_t = std::packaged_task<typename std::result_of<F(Args...)>::type ()>;
5352

5453
std::shared_ptr<packaged_task_t> task(new packaged_task_t(

0 commit comments

Comments
 (0)