代码拉取完成,页面将自动刷新
//
// 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));
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。