如何实现线程对向量的向量进行同步排序? [英] How to implement threads to sort a vector of vectors synchronously?

查看:70
本文介绍了如何实现线程对向量的向量进行同步排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a vector that contains vectors of integers, the idea is to sort each vector of integers by calling the bubble sort with threads for each one and then print the time of execution.

我试图在每次迭代中实现一个线程,但是不起作用

I'm trying to implement a thread in each iteration but doesn't work


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必须更快时,这两种方法都花费了相同的时间.

It's taken the same time in both methods, when sinchronousSort must be more fast.

推荐答案

您需要在循环后保留 thread s并 join .

You need to preserve the threads and join them after the loop.

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 ;
}

请注意,这实际上不是线程池.线程池是您保留并分配作业的线程池.这只是一堆 thread .还要注意,如果线程池要穿红色和黑色并与观众交谈,那您将面临一个真正的问题.寻求即时帮助.

Note that this isn't really a threadpool. A threadpool is a pool of threads you keep around and assign jobs. This is just a bunch of threads. Also note that if the threadpool takes to wearing red and black and conversing with the audience, you have a real problem. Seek immediate assistance.

这篇关于如何实现线程对向量的向量进行同步排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆