QQ网名大全

线程池的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 18:43:52
最佳回答
类似问题(10)
  • 佚名
    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...