Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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
Boost线程串行运行,而不是并行运行 我是一个完全的新手,可以在C++中多线程,并决定从Boost库开始。此外,我使用英特尔的C++编译器(从Studio Studio 2011)到Vista上的VS2010。_C++_Multithreading_Parallel Processing_Boost Thread - Fatal编程技术网

Boost线程串行运行,而不是并行运行 我是一个完全的新手,可以在C++中多线程,并决定从Boost库开始。此外,我使用英特尔的C++编译器(从Studio Studio 2011)到Vista上的VS2010。

Boost线程串行运行,而不是并行运行 我是一个完全的新手,可以在C++中多线程,并决定从Boost库开始。此外,我使用英特尔的C++编译器(从Studio Studio 2011)到Vista上的VS2010。,c++,multithreading,parallel-processing,boost-thread,C++,Multithreading,Parallel Processing,Boost Thread,我正在编写一个遗传算法,并希望利用多线程的优点:我希望为群体中的每个个体(对象)创建一个线程,以便它们并行计算适合度(繁重的操作),以减少总执行时间 据我所知,每当我启动一个子线程时,它就会“在后台”工作,而父线程会继续执行下一条指令,对吗?因此,我想创建并启动我需要的所有子线程(在for循环中),然后等待它们完成(在另一个for循环中调用每个线程的join()),然后继续 我面临的问题是,在新创建的线程完成工作之前,第一个循环不会继续到下一个迭代。然后,第二个循环就消失了,因为在该循环被命中时

我正在编写一个遗传算法,并希望利用多线程的优点:我希望为群体中的每个个体(对象)创建一个线程,以便它们并行计算适合度(繁重的操作),以减少总执行时间

据我所知,每当我启动一个子线程时,它就会“在后台”工作,而父线程会继续执行下一条指令,对吗?因此,我想创建并启动我需要的所有子线程(在
for
循环中),然后等待它们完成(在另一个
for
循环中调用每个线程的
join()
),然后继续

我面临的问题是,在新创建的线程完成工作之前,第一个循环不会继续到下一个迭代。然后,第二个循环就消失了,因为在该循环被命中时,所有线程都已连接

这里是(我认为是)相关的代码片段。告诉我你还有什么需要知道的

class Poblacion {
    // Constructors, destructor and other members
    // ...
    list<Individuo> _individuos;
    void generaInicial() { // This method sets up the initial population.
        int i;
        // First loop
        for(i = 0; i < _tamano_total; i++) {
            Individuo nuevo(true);
            nuevo.Start(); // Create and launch new thread
            _individuos.push_back(nuevo);
        }

        // Second loop
        list<Individuo>::iterator it;
        for(it = _individuos.begin(); it != _individuos.end(); it++) {
            it->Join();
        }

        _individuos.sort();
    }
};

谢谢大家!<代码>:D

如果这是您的真实代码,那么您就有问题了

    for(i = 0; i < _tamano_total; i++) {
        Individuo nuevo(true);
        nuevo.Start(); // Create and launch new thread
        _individuos.push_back(nuevo);
    }

    void Start() {
        _hilo = boost::thread(&Individuo::Run, this);
    }

用Boost.threads做这样的事情很有趣,但是OpenMP会让它变得更有趣easier@CharlesB我认为对于这种特殊情况,问题已经解决了,但我将研究OpenMP。谢谢你的建议。是的!这正是问题所在。我确信这与
list.push_back()。但我离题了。。。非常感谢你!
    for(i = 0; i < _tamano_total; i++) {
        Individuo nuevo(true);
        nuevo.Start(); // Create and launch new thread
        _individuos.push_back(nuevo);
    }

    void Start() {
        _hilo = boost::thread(&Individuo::Run, this);
    }
    for(i = 0; i < _tamano_total; i++) {
        _individuos.push_back(Individuo(true)); // add new entry to list
        _individuos.back().Start(); // start a thread for that entry
    }