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
C++ 如何实现线程对向量向量进行同步排序?_C++_Multithreading_Asynchronous_Threadpool_Synchronous - Fatal编程技术网

C++ 如何实现线程对向量向量进行同步排序?

C++ 如何实现线程对向量向量进行同步排序?,c++,multithreading,asynchronous,threadpool,synchronous,C++,Multithreading,Asynchronous,Threadpool,Synchronous,我正在创建一个包含整数向量的向量,其思想是通过调用带线程的冒泡排序对每个整数向量进行排序,然后打印执行时间 我试图在每次迭代中实现一个线程,但没有成功 vector<int> bubbleSort(vector<int>); void asynchronousSort(vector<vector<int>> pool){ double executionTime; clock_t tStart = clock(); f

我正在创建一个包含整数向量的向量,其思想是通过调用带线程的冒泡排序对每个整数向量进行排序,然后打印执行时间

我试图在每次迭代中实现一个线程,但没有成功


vector<int> bubbleSort(vector<int>);

void asynchronousSort(vector<vector<int>> pool){
    double executionTime;

    clock_t tStart = clock();
    for(int i = 0; i < pool.size(); i++){
        thread t (bubbleSort, pool[i]);
        t.join();
    }

    executionTime = (double)(clock() - tStart)/CLOCKS_PER_SEC;
    cout << "Time :" << executionTime<< "s." << endl ;
}

void synchronousSort(vector<vector<int>> pool){
    double executionTime;
    clock_t tStart = clock();

    for(int i = 0; i < pool.size(); i++){
        pool[i] = bubbleSort(pool[i]);
    }

    executionTime = (double)(clock() - tStart)/CLOCKS_PER_SEC;
    cout << "Time :" << executionTime<< "s." << endl ;
}

int main(int argc, const char * argv[]) {

    int selectMethod;
    vector<vector<int>> pool(10);
    //Create 10 lists with 10000 numbers in decrement.
    for (int i = 0; i < 10; i++) {
        vector<int> temp;
        for(int j = 10000; j > 0; j--){
            temp.push_back(j);
        }
        pool.push_back(temp);
    }

    cout << "Select method 1)Asynchronously. 2)Synchronously. (1/2): ";
    cin >> selectMethod;

    if(selectMethod == 1){
        asynchronousSort(pool);
    }else{
        synchronousSort(pool);
    }
    return 0;
}


这两种方法都需要相同的时间,此时sinchronousSort必须更快。

在for循环中,等待线程完成t.join,因此没有同步排序

for(int i = 0; i < pool.size(); i++){
        thread t (bubbleSort, pool[i]);
        t.join();
    }
改用detach,然后在函数返回之前等待所有线程,例如,将线程*存储在一个向量中,然后在for循环中连接所有线程


也就是说,线程创建需要时间,因此它可能没有您认为的快速操作的速度快。如果这是针对Windows的,您可以使用my类,有文档记录。

在for循环中,您等待线程完成t.join,因此没有同步排序

for(int i = 0; i < pool.size(); i++){
        thread t (bubbleSort, pool[i]);
        t.join();
    }
改用detach,然后在函数返回之前等待所有线程,例如,将线程*存储在一个向量中,然后在for循环中连接所有线程

也就是说,线程创建需要时间,因此它可能没有您认为的快速操作的速度快。如果这是针对Windows的,您可以使用my类,有文档记录。

您需要保留线程并在循环后加入它们

void asynchronousSort(vector<vector<int>> pool){
    double executionTime;
    vector<thread> threads;

    clock_t tStart = clock();
    for(int i = 0; i < pool.size(); i++){
        threads.emplace_back(bubbleSort, pool[i]); // make threads directly inside the vector
                                                   // saves having to std::move them
    }
    for(thread & t:threads) // now that all the threads are up (and maybe running), join them
    {
        t.join();
    }
    // now we can get the execution time
    // note: unless pool is large you may miss it. 
    executionTime = (double)(clock() - tStart)/CLOCKS_PER_SEC;
    cout << "Time :" << executionTime<< "s." << endl ;
}
请注意,这并不是一个真正的线程池。线程池是一个线程池,您可以保留这些线程并分配作业。这只是一堆线程。还要注意的是,如果threadpool喜欢穿红色和黑色的衣服,并与观众交谈,那么你就有了一个真正的问题。请立即寻求帮助。

您需要保留线程并在循环后加入它们

void asynchronousSort(vector<vector<int>> pool){
    double executionTime;
    vector<thread> threads;

    clock_t tStart = clock();
    for(int i = 0; i < pool.size(); i++){
        threads.emplace_back(bubbleSort, pool[i]); // make threads directly inside the vector
                                                   // saves having to std::move them
    }
    for(thread & t:threads) // now that all the threads are up (and maybe running), join them
    {
        t.join();
    }
    // now we can get the execution time
    // note: unless pool is large you may miss it. 
    executionTime = (double)(clock() - tStart)/CLOCKS_PER_SEC;
    cout << "Time :" << executionTime<< "s." << endl ;
}

请注意,这并不是一个真正的线程池。线程池是一个线程池,您可以保留这些线程并分配作业。这只是一堆线程。还要注意的是,如果threadpool喜欢穿红色和黑色的衣服,并与观众交谈,那么你就有了一个真正的问题。请立即寻求帮助。

如果您有支持VS2017等执行策略的C++17编译器,请使用std::for_eachstd::execution::par。。。可能是一种选择

#include <algorithm>
#include <execution>

void asynchronousSort(vector<vector<int>>& pool) {
    std::for_each(std::execution::par, pool.begin(), pool.end(), [](auto& v) {
        // used std::sort instead
        std::sort(v.begin(), v.end());
    });
}

如果您有一个支持执行策略(如VS2017)的C++17编译器,使用std::for_eachstd::execution::par。。。可能是一种选择

#include <algorithm>
#include <execution>

void asynchronousSort(vector<vector<int>>& pool) {
    std::for_each(std::execution::par, pool.begin(), pool.end(), [](auto& v) {
        // used std::sort instead
        std::sort(v.begin(), v.end());
    });
}

就这个问题达成一致。在解决方案上存在分歧。如果询问者分离,程序很有可能在线程完成之前退出。为需要的边缘案例保存分离,将线程存储在向量中,并在所有线程都启动并运行后连接它们。更正,添加注释。对问题达成一致。在解决方案上存在分歧。如果询问者分离,程序很有可能在线程完成之前退出。为需要的边缘案例保存分离,将线程存储在一个向量中,并在它们全部启动并运行后连接它们。正确,添加注释。相关:除非你对一些很小的东西进行排序,我指的是十几个插槽,单词的速度必须更快,bubblesort不应在同一算法中重合,除非单词“从不使用”位于后者之前。也就是说,函数名是相反的。异步应该是线程化的,同步应该是直接调用的。你应该建立一个线程向量,每个线程都有一个向量进行排序,然后在所有线程都建立起来后将它们连接起来。或者更好的是,使用一个池和一个输入队列。相关:除非你在对一些很小的东西进行排序,我说的很小,可能是十几个插槽,否则单词必须更快,bubblesort不应该在同一个算法中重合,除非单词“从不使用”在后者之前。也就是说,函数名是相反的。异步应该是线程化的,同步应该是直接调用的。你应该建立一个线程向量,每个线程都有一个向量进行排序,然后在所有线程都建立起来后将它们连接起来。或者更好的方法是使用池和输入队列。