Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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-asio的线程_C++_Multithreading_Boost - Fatal编程技术网

C++ C++;带boost-asio的线程

C++ C++;带boost-asio的线程,c++,multithreading,boost,C++,Multithreading,Boost,我有一个使用boosts库的线程池,我在下面的示例中设置了两个线程,它们可以重新运行4次。我可以检查的最佳方法是_service,查看是否所有子线程都已执行完毕,然后才能继续编写代码?代码的其余部分取决于在程序继续之前完成的所有子线程。如果我进行Sleep(1000)调用,我可以获得所需的行为,但这是不可取的,我查看了io\u service\uu.stopped()上的检查,但总是返回0。任何想法都将不胜感激 #include <iostream>

我有一个使用boosts库的线程池,我在下面的示例中设置了两个线程,它们可以重新运行4次。我可以检查
的最佳方法是_service
,查看是否所有子线程都已执行完毕,然后才能继续编写代码?代码的其余部分取决于在程序继续之前完成的所有子线程。如果我进行
Sleep(1000)
调用,我可以获得所需的行为,但这是不可取的,我查看了
io\u service\uu.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");
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类模型{
公众:
//建造师
模型(){
work\u ctrl\uu=newboost::asio::io\u服务::work(io\u服务);
对于(int i=0;i<2;++i){
线程。创建线程(
boost::绑定(&boost::asio::io_服务::运行,&io_服务);
}
}
//解构器
~Model(){
删除工作\u ctrl\u;
}
//我想要线程的函数
无效向量(无符号开始,无符号最后){

您可能想知道工作何时完成,而不是线程何时停止执行(直到您调用
io\u service.stop()
或删除
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; });
    }
}
std::mutex mutex;
std::条件\可变条件;
整数工作计数;
无效向量(无符号开始,无符号最后){

cout如果对当前线程中正在运行的某些工作没有问题,可以调用
io\u service\uu.run()
删除
工作\u ctrl\ucode>后要等待的位置。当它返回时,没有剩余的工作,您可以安全地继续。您可以将线程组中的并发级别降低1,以获得相同的总体并发性


一个简单的替代方法是调用
threads\uu.join\uall()
在所有帖子排队后,您删除了
work\u ctrl\u

我不确定您的意思,我的印象是,如果您加入线程,您就不能像我的示例中那样重新加入。@Cyrillm\u 44是的,在加入线程后,您必须创建新的线程