Слияние кода завершено, страница обновится автоматически
//
// Created by Administrator on 2015/5/26.
//
#pragma once
#include <vector>
#include <queue>
#include <mutex>
#include <thread>
#include <future>
#include <functional>
#include <condition_variable>
namespace esvr
{
class ThreadPool{
public:
ThreadPool(size_t);
~ThreadPool();
template<typename F, typename... Args>
auto enqueue(F&& f, Args&&... args)->std::future < typename std::result_of<F(Args...)>::type > ;
void destroy();
private:
std::vector<std::thread> workers;
std::queue<std::function<void()>> tasks;
private:
std::mutex queue_mutex;
std::condition_variable condition;
volatile bool stop{ false };
};
inline ThreadPool::ThreadPool(size_t threads)
{
threads = threads < 2 ? 2 : threads;
for (size_t i = 0; i < threads; ++i)
{
workers.emplace_back(std::thread(
[this]
{
for (; !this->stop;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(queue_mutex);
this->condition.wait(lock, [this]{return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) break;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
)
);
}
}
template<typename F, typename... 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);
if (!stop) tasks.emplace([task](){(*task)(); });
}
condition.notify_one();
return res;
}
inline ThreadPool::~ThreadPool()
{
this->destroy();
}
void ThreadPool::destroy()
{
{
//std::unique_lock<std::mutex> lock(queue_mutex); msvc2013 dead lock.
if (stop) return;
stop = true;
}
condition.notify_all();
for (auto& worker : workers)
{
if (worker.joinable()) worker.join();
}
workers.clear();
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
};
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )