【发布时间】:2018-06-21 06:38:31
【问题描述】:
我有一个使用 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");
}
【问题讨论】:
标签: c++ multithreading boost