Boost Asio:等待thread_group处理完所有发布的任务
Boost Asio: waiting until thread_group has processed all posted tasks?
我有一个对象,一旦创建,它就会在后台执行许多任务,但应该阻止直到l/all/posted任务完成。即:
struct run_many{
boost::asio::io_service m_io_service;
boost::thread_group m_threads;
boost::asio::signal_set m_signals;
void evaluate(std::string work, int i){ /*...*/ }
void run_tasks(int tasks, std::string work){
{
boost::asio::io_service::work w(m_io_service); //
for(int i=0;i<tasks;i++)
m_io_service.post(boost::bind(&run_many::evaluate, this, work, i));
}
//m_io_service.run(); // blocks forever
m_io_service.stop(); // seems to cut off queued jobs
m_threads.join_all(); // works only after m_io_service.stop()
}
run_many(int n_workers)
{
m_threads.create_thread(boost::bind(&boost::asio::io_service::run,m_io_service);
}
};
所以我被卡住了。。。似乎我可以永远等待,也可以在每个线程中当前运行的作业之后切断队列。文件里一定有我遗漏的东西?
根据文档,这个想法应该有效(伪代码):
...
// put a few handlers into io_service
...
// don't forget to destroy all boost::asio::io_service::work objects
while (m_io_service.stopped())
{
m_io_service.run();
}
// when code reaches this line m_io_service will be in stopped state and all handlers will be executed
// this code can be called without any doubts about being locked
m_threads.join_all();
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- openMp 动态调度与按处理时间排序任务时的 LPT 调度相同吗?
- 即使在多任务处理时,添加用户时间 + 系统时间(来自 shell 的时间命令)是否是一种可靠的措施?
- 任务计划程序库的预处理不起作用 - 多定义错误
- wxWidgets-单线程多任务处理
- 如何处理任务,同时剩余的线程完成工作
- 以下数据处理任务是否适合GPU计算
- 如何在后台进程上处理来自Windows任务管理器的"End Task"?
- Boost Asio:等待thread_group处理完所有发布的任务
- 多任务处理在我的程序中不起作用 C++11
- 如何卸载高度依赖于正确异常处理的计算繁重的任务
- 如何在c++中处理阻塞调用的并发任务
- 异常处理,WinRT c++并发异步任务
- 如何在QThread中正确处理来自长时间运行的任务的信号和事件
- C++线程多任务处理[窗口]
- 如何在C或c++中进行多任务处理?