1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/caipengyang-esvr

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
В этом репозитории не указан файл с открытой лицензией (LICENSE). При использовании обратитесь к конкретному описанию проекта и его зависимостям в коде.
Клонировать/Скачать
es_thread_pool.h 2.1 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
yangyang Отправлено 15.04.2016 06:31 ac3a4e6
//
// 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 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/caipengyang-esvr.git
git@api.gitlife.ru:oschina-mirror/caipengyang-esvr.git
oschina-mirror
caipengyang-esvr
caipengyang-esvr
master