1

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

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

1
  • 5
    Related: Unless you're sorting something tiny (and by tiny i mean perhaps a dozen slots), the words "must be more fast" and "bubblesort" should never coincide in the same algorithm unless the words "never use" precedes the latter. That said, your function names are opposed. The async should be threaded, the synch should be direct-invoke. you should stand up a vector of threads, each getting a vector to sort, and then join them all after they're all stood up. Or better still, use a pool and an input queue. Commented Jan 21, 2019 at 21:30

3 Answers 3

3

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

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.

Sign up to request clarification or add additional context in comments.

Comments

3

If you have a C++17 compiler supporting execution policies (like VS2017), using std::for_each(std::execution::par, ... may be an option.

#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()); }); } 

Comments

1

In your for loop, you wait for a thread to finish (t.join()), so there is no simultaneous sorting.

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

Use detach() instead, then wait for all threads before the function returns (by e.g. storing threads* in a vector then join all of them in a for loop).

That said, thread creation takes time and therefore it might not be as fast as you think for quick operations. If this is for Windows, you can use my Threadpool API class, documented here.

2 Comments

Agree on problem. Disagree on solution. If the asker detaches, odds are good that the program will exit before the threads finish. Save detach for the edge cases that need it, store the threads in a vector, and join them after they are all started and running.
Correct, added comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.