线程池的C++11版本实现问题
因为你没有贴上全部代码,也很难看出来
看一下这个C++11的thread pool怎么样?
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~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;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
{
for(;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
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
-
佚名2024-05-23 07:30:45
问Linux c如何创建线程池
答linux c 并没有自带的线程池,纯C的线程池很少1:使用glib的线程池,gthreadpool,这个是linux C 下面的一个线程池实现,可以用于生产环...
-
佚名2024-05-23 05:34:29
问C语言中的线程?
答给你推荐一些比较好的教程吧,你应该用得着: 漫谈C++ Builder多线程编程技术: http:///html/xueyuan/chengxukaifa/...
-
佚名2024-05-23 10:11:03
问c多线程有几种实现方法
答直接使用操作系统API使用标准C++线程支持库(可跨平台)使用第三方线程库
-
佚名2024-05-23 04:56:31
问C语言多线程实现
答多线程随机选号程序 以下程序运行后看起来比较有意思,像一个随机选号程序,但不是完全按照问题所说的写的 可供参考,要改很容易//多线程随机选号程序示例#inclu...
-
佚名2024-05-23 04:50:12
问c语言多线程
答main(){if(!fork()) { //代码 //...新线程,与原线程共享数据空间 }else { //代码 //..原线...
-
佚名2024-05-23 20:22:27
问c 多线程 问题
答这还不简单,是没有解决同步的问题。 操作系统执行指令是由时间片组成的。你那个变量i,又没有规定只能一个人访问。第一个线程执行了,i=0,时间片到了,轮到第二个线...
-
佚名2024-05-23 23:10:57
问C语言多线程编程的一个菜鸟问题
答由于是多线程环境,放一个原子变量,每次调用该函数时,让该原子变量的值递增,同时用一个全局变量记录当该变量值为初始值时的值。不知道能明白我的意思没不用原子操作,或...
-
佚名2024-05-23 08:00:00
问关于三道C语言编程的问题
答1.先给出第一个问题的答案//Date.hvoid DateMake(unsigned int date, int* month, int* day, int*...
-
佚名2024-05-23 08:00:00
问C#中的多线程问题用于什么地方?怎么掌握?
答1.每个窗体都有自己的都在不同的线程上运行,如果需要在窗体之间交互,就需要在线程之间交互。2.当线程Sleep时,系统就退出执行队列一段时间,当睡眠结束时,系统...
-
佚名2024-05-23 08:00:00
问求 C# 多线程 实例
答//应为要判断上一个操作是否完成!用异步调用! //主线程中启动一个支线程,执行doSomething这样的一个方法。 Thread...
风水
起名
网名
- 1 网名关于圣经
- 2 带江字和琴字的网名
- 3 lol的qq网名大全
- 4 四川话内涵网名
- 5 旺旺号 昵称
- 6 ih5搜集微信昵称
- 7 知道王者荣耀的昵称怎么加好友
- 8 刺激战场可用符号昵称
- 9 淘宝直播间粉丝的昵称怎么改
- 10 适合篮球的昵称
说说
- 1 女孩暗恋男孩的个性签名
- 2 qq个性签名怎样变彩色字
- 3 快手个性签名模板努力
- 4 男生五字网名个性签名大全霸气
- 5 保护别人的说说
- 6 不想联系所有人的说说
- 7 喜欢你又不敢说的说说
- 8 关于彼此相爱互相等待的说说
- 9 关于不要害怕的说说
- 10 小班毕业爸妈说说