Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用c+;中的boost在所有任务完成后才将新任务添加到threadpool+; boost::asio::io\u服务ioService; boost::线程组线程池; 升压:势垒(5); boost::asio::io_service::work-work(ioService); 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程1 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程2 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程3线程池。创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程4 while(true) { { boost::lock\u guard lock(互斥锁); 迭代器it=m_map.begin(); while(it!=m_map.end()) { post(boost::bind(&ProcessFun,this,it)); ++它; } ioService.run();_C++_Multithreading_Boost_Threadpool_Boost Asio - Fatal编程技术网

C++ 如何使用c+;中的boost在所有任务完成后才将新任务添加到threadpool+; boost::asio::io\u服务ioService; boost::线程组线程池; 升压:势垒(5); boost::asio::io_service::work-work(ioService); 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程1 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程2 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程3线程池。创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程4 while(true) { { boost::lock\u guard lock(互斥锁); 迭代器it=m_map.begin(); while(it!=m_map.end()) { post(boost::bind(&ProcessFun,this,it)); ++它; } ioService.run();

C++ 如何使用c+;中的boost在所有任务完成后才将新任务添加到threadpool+; boost::asio::io\u服务ioService; boost::线程组线程池; 升压:势垒(5); boost::asio::io_service::work-work(ioService); 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程1 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程2 创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程3线程池。创建_线程(boost::bind(&boost::asio::io_服务::run,&ioService))//线程4 while(true) { { boost::lock\u guard lock(互斥锁); 迭代器it=m_map.begin(); while(it!=m_map.end()) { post(boost::bind(&ProcessFun,this,it)); ++它; } ioService.run();,c++,multithreading,boost,threadpool,boost-asio,C++,Multithreading,Boost,Threadpool,Boost Asio,最简单的方法是根据boost asio文档调用ioService.run() io_服务对象的run()函数在工作停止时不会退出 正在进行中。当没有未完成的工作时,它确实会退出 剩下的 顺便说一句,在看不到更多程序的情况下,很难确定这一点,但似乎您正试图挫败asio的主要目的。您正在序列化任务批。如果在批处理2中的任何任务开始之前完全处理批处理1中的所有任务非常重要,那么这可能是有意义的,但是这是一个奇怪的用法 还要小心,如果batch#1任务的任何处理程序尝试添加新任务,它们可能会在尝试获取互

最简单的方法是根据boost asio文档调用ioService.run()

io_服务对象的run()函数在工作停止时不会退出 正在进行中。当没有未完成的工作时,它确实会退出 剩下的

顺便说一句,在看不到更多程序的情况下,很难确定这一点,但似乎您正试图挫败asio的主要目的。您正在序列化任务批。如果在批处理2中的任何任务开始之前完全处理批处理1中的所有任务非常重要,那么这可能是有意义的,但是这是一个奇怪的用法


还要小心,如果batch#1任务的任何处理程序尝试添加新任务,它们可能会在尝试获取互斥锁时死锁。

因此,我的最终解决方案是自己创建一个小信号量,其中包含一个
互斥锁和条件veritable
我在这里发现:

我将此信号量作为指向线程的指针传递,并在每次迭代中重置它。 我必须修改一点信号量代码以启用重置功能,因为我的线程有时会在主线程进入睡眠状态之前完成工作,所以我必须修改abit中的条件

boost::asio::io_service ioService;
boost::thread_group threadpool;
boost::barrier barrier(5);
boost::asio::io_service::work work(ioService);
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));  //Thread 1
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));  //Thread 2
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));  //Thread 3 threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));  //Thread 4

while (true)
{
  { 
     boost::lock_guard<boost::recursive_mutex> lock(mutex_);
     map::iterator it = m_map.begin();   
     while (it != m_map.end())
     {
         ioService.post(boost::bind(&ProcessFun, this, it));
         ++it;    
     }

    ioService.run(); <-- main thread is stuck here..  
   }

}
类信号量
{
私人:
boost::mutex mutex;
boost::条件u可变条件u;
无符号长计数;
公众:
信号量()
:count_uuz()
{}
无效重置(整数x)
{
计数=x;
}
作废通知()
{
boost::mutex::作用域锁定(mutex);
++伯爵;
如果(计数<1)
条件u.通知u one();
}
无效等待()
{
boost::mutex::作用域锁定(mutex);
而(计数>1)
条件等待(锁定);
}
};
....
....
信号量*m_信号量=新信号量();
while(true)
{
{ 
boost::lock\u guard lock(互斥锁);
迭代器it=m_map.begin();
if(it!=m_map.end())
m_信号量->重置(m_map.size());
while(it!=m_map.end())
{
post(boost::bind(&ProcessFun,this,it,&m_信号量));
++它;
}
m_信号量。等待();
}
}

谢谢!是的,我已经考虑到了这一点,正如你所说,我想在开始新的一批之前完成第一批。否则就没有用:)@USer22999299似乎是合法的,如果不破坏
io\u服务::work
实例并调用
io\u服务::stop(),你就不会退出
io\u服务::run()
@Chnossos所以这似乎不是我可以使用的解决方案。我正在寻找一种方法,让主线程等待所有线程完成工作。只有这样,添加新任务时,解决方案才简单,您不需要使用
io_服务::work
,这样
io_服务::run
就会自然退出当所有任务都完成执行时。您的线程也需要在自定义条件下等待,以防止它们过早退出。@Chnossos,捕捉得好。我错过了io_服务::work.usnnnn,io_服务::work的目的是防止您想要的行为。
class semaphore
{
private:
    boost::mutex mutex_;
    boost::condition_variable condition_;
    unsigned long count_;

public:
    semaphore()
        : count_()
    {}

    void reset(int x)
    {
      count = x;
    }
    void notify()
    {
        boost::mutex::scoped_lock lock(mutex_);
        ++count_;
        if(count_ < 1)
            condition_.notify_one();
    }

    void wait()
    {
        boost::mutex::scoped_lock lock(mutex_);
        while(count_ > 1)
            condition_.wait(lock);

    }
};


....
....
semaphore*  m_semaphore = new semaphore();

while (true)
{
  { 
     boost::lock_guard<boost::recursive_mutex> lock(mutex_);
     map::iterator it = m_map.begin();
     if(it ! = m_map.end())
        m_semaphore->reset(m_map.size());  
     while (it != m_map.end())
     {
         ioService.post(boost::bind(&ProcessFun, this, it, &m_semaphore));
         ++it;    
     }

      m_semaphore.wait();
   }

}