c++ - C++ 线程与 boost asio
问题描述
我有一个使用 boosts 库的线程池,我在下面的示例中设置了运行两个重新运行 4 次的线程。在继续编写代码之前,我可以检查is_service
所有子线程是否已完成的最佳方法是什么?其余代码取决于所有子线程在程序继续之前完成。如果我拨打电话,我可以获得所需的行为,Sleep(1000)
但这是不可取的,我查看了一个检查io_service_.stopped()
但总是返回一个0
. 任何想法将不胜感激。
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
#include <vector>
#include <string>
using namespace std;
class Model {
public:
// Constructor
Model() {
work_ctrl_ = new boost::asio::io_service::work(io_service_);
for (int i = 0; i < 2; ++i) {
threads_.create_thread(
boost::bind(&boost::asio::io_service::run, &io_service_));
}
}
// Deconstructor
~Model() {
delete work_ctrl_;
}
// Function I want to thread
void manipulate_vector(unsigned start, unsigned last) {
cout << "entering manipulate vector(), from thread " << boost::this_thread::get_id() << endl;
for(unsigned k = start; k <= last; ++k)
my_vector_[k] *= sqrt(32);
cout << "exit manipulate vector()" << endl;
Sleep(500); // Add a sleep to mimic a long algorithm being executed
}
void update() {
// Do otherstuff that can't be threaded
cout << "entering update" << endl;
// run manipulate_vector() across multiple threads
// - start thread
// - execute function call.
// - stop thread
io_service_.post(boost::bind(manipulate_vector, this, 0, mid_point_));
io_service_.post(boost::bind(manipulate_vector, this, mid_point_, my_vector_.size()));
cout << io_service_.stopped() << endl;
// keep doing otherstuff that can't be threaded
cout << "hopefully the threads are finished and I can take that information and continue." << endl;
}
void run(void) {
// call update 10 times
for(unsigned i = 0; i < 4; ++i) {
update();
//Sleep(2000);
}
}
void initialise() {
// initialise vector
for(unsigned j = 0; j < 100000000; ++j)
my_vector_.push_back(j);
mid_point_ = 49999999;
}
private:
boost::asio::io_service io_service_;
boost::thread_group threads_;
boost::asio::io_service::work *work_ctrl_;
unsigned n_threads_;
vector<double> my_vector_;
unsigned mid_point_;
};
int main() {
std::cout << "----------Enter Main----------" << std::endl;
Model model;
model.initialise();
model.run();
std::cout << "----------Exit Main----------" << std::endl;
system("PAUSE");
}
解决方案
大概您想知道您的工作何时完成,而不是线程何时停止执行(直到您调用io_service.stop()
或 delete才会知道work-ctrl_
)。
标准解决方案是使用条件变量:
std::mutex mutex;
std::condition_variable condition;
int workCount;
void manipulate_vector(unsigned start, unsigned last) {
cout << "entering manipulate vector(), from thread " << boost::this_thread::get_id() << endl;
for(unsigned k = start; k <= last; ++k)
my_vector_[k] *= sqrt(32);
cout << "exit manipulate vector()" << endl;
Sleep(500); // Add a sleep to mimic a long algorithm being executed
std::unique_lock<std::mutex> lock(mutex);
workCount--;
condition.notify_one();
}
void update() {
// Do otherstuff that can't be threaded
cout << "entering update" << endl;
// run manipulate_vector() across multiple threads
// - start thread
// - execute function call.
// - stop thread
workCount = 2;
io_service_.post(boost::bind(manipulate_vector, this, 0, mid_point_));
io_service_.post(boost::bind(manipulate_vector, this, mid_point_, my_vector_.size()));
{
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [&]{ return workCount == 0; });
}
}
推荐阅读
- python - 如何进行从 JSON 格式到表格格式的特征映射(pop)?
- python - 不使用 urllib.request 处理互联网
- ibm-doors - 替换dxl中两个模块之间的字符串
- javascript - AngluarJS 和 FileReader 如何将文件保存到路径
- python - 删除字符串池
- android - Gradle 插件版本与领域 gradle 版本 Android Studio 冲突
- html - 离子选择不从 ng-model 中选择多个值
- python - 其他语言的 Python 月/日名称
- demandware - 从 BonusDiscountLineItem 需求软件中过滤掉缺货产品
- iis - 在 IIS 上托管 ionic 3 应用程序?