[BUG] Thread pool is NOT avaliable when it was created.
poor1017 opened this issue · 1 comments
The thread pool is NOT avaliable when it was created.
void create_threads(const std::function<void()>& init_task)
{
{
const std::scoped_lock tasks_lock(tasks_mutex);
tasks_running = thread_count;
workers_running = true;
}
for (concurrency_t i = 0; i < thread_count; ++i)
{
threads[i] = std::thread(&thread_pool::worker, this, i, init_task);
}
}
Because of the statement: tasks_running = thread_count;
, the thread pool is full when it was created, tasks_running
will decrease one by one. This behavior is not consistent with the code last version.
Hi @bshoshany , why the change? Thank you!
Hi @poor1017 and thanks for opening this issue! However, this is not a bug. In v4.0.0 I added the option to run a thread initialization function, so when worker()
starts running in each thread, it executes this function and then decreases the number of running tasks by 1. This allows the user to wait for the thread initialization function to run in all the threads using wait()
, to ensure that no tasks run before all threads are initialized, in case, for example, the initialization function is used to allocate resources to each thread.