- new thread pool (no stucks anymore)

This commit is contained in:
Dmytro Bogovych 2020-05-08 17:05:56 +03:00
parent 0d69c26a13
commit 086581a2b1
2 changed files with 66 additions and 83 deletions

View File

@ -0,0 +1,51 @@
#include "HL_ThreadPool.h"
thread_pool::thread_pool(size_t num_of_threads, const std::string& name)
{
this->name = name;
if (!num_of_threads)
num_of_threads = std::thread::hardware_concurrency();
for(size_t idx = 0; idx < num_of_threads; idx++)
this->workers.push_back(std::thread(&thread_pool::run_worker, this));
}
// Add new work item to the pool
void thread_pool::enqueue(const thread_pool::task& t)
{
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->tasks.push(t);
}
this->condition.notify_one();
}
// the destructor joins all threads
thread_pool::~thread_pool()
{
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
stop = true;
}
this->condition.notify_all();
for(std::thread &worker: workers)
worker.join();
}
void thread_pool::run_worker()
{
ThreadHelper::setName(this->name);
task t;
while(!this->stop)
{
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock, [this]{return !this->tasks.empty() || this->stop;});
t = this->tasks.front();
this->tasks.pop();
}
t(); // function<void()> type
}
}

View File

@ -13,99 +13,31 @@
#include <optional>
#include "HL_Sync.h"
class ThreadPool
class thread_pool
{
public:
ThreadPool(size_t, const std::string&);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
void setPause(bool v) { this->pause = v;}
typedef std::function<void()> task;
thread_pool(size_t num_of_threads, const std::string&);
~thread_pool();
void enqueue(const task& task);
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;
std::queue< task > tasks;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop, pause;
bool stop = false;
// thread name prefix for worker threads
std::string name;
void run_worker();
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads, const std::string& name)
: stop(false), pause(false), name(name)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this, i]
{
ThreadHelper::setName(this->name + std::to_string(i));
for(;;)
{
std::function<void()> task; bool task_assigned = false;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->pause || !this->tasks.empty(); });
if (this->tasks.empty())
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if(this->stop && this->tasks.empty())
return;
if(this->pause)
continue;
if (this->tasks.size())
{
task = std::move(this->tasks.front()); task_assigned = true;
this->tasks.pop();
}
}
if (task_assigned)
task();
}
}
);
}
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
worker.join();
}
#endif