1 Star 0 Fork 22

nirvanaReborn_cpp/common_source_cpp

forked from 10km/common_source_cpp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ThreadPool.h 1.61 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* ThreadPool.h
*
* open source From:https://github.com/progschj/ThreadPool
*/
#ifndef COMMON_SOURCE_CPP_THREADPOOL_H_
#define COMMON_SOURCE_CPP_THREADPOOL_H_
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
namespace gdface{
class ThreadPool {
public:
const size_t concurrency;
ThreadPool(size_t);
// add new work item to the pool
template<class F, class... Args>
auto 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;
}
~ThreadPool();
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;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
} /* namespace gdface */
#endif /* COMMON_SOURCE_CPP_THREADPOOL_H_ */
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/nirvana-reborn_cpp/common_source_cpp.git
[email protected]:nirvana-reborn_cpp/common_source_cpp.git
nirvana-reborn_cpp
common_source_cpp
common_source_cpp
master

搜索帮助