Skip to content

Instantly share code, notes, and snippets.

@illescasDaniel
Created October 24, 2016 11:26
Show Gist options
  • Save illescasDaniel/f62e7a73045e6abefc3372b88cf7c2e2 to your computer and use it in GitHub Desktop.
Save illescasDaniel/f62e7a73045e6abefc3372b88cf7c2e2 to your computer and use it in GitHub Desktop.
Concurrent programming (sort vectors) [C++]
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include <cstddef>
using namespace std;
using namespace chrono;
void sortVector() {
constexpr size_t size = 10000000;
srand(uint(time(nullptr)));
unique_ptr<int[]> vector(new int[size]);
for (size_t idx = 0; idx < size; ++idx) {
vector[idx] = rand();
}
sort(vector.get(), vector.get() + size);
}
int main() {
auto concurrent_1 = high_resolution_clock::now();
thread t1(sortVector);
thread t2(sortVector);
thread t3(sortVector);
t1.join();
t2.join();
t3.join();
auto concurrent_2 = high_resolution_clock::now();
cout << float(duration_cast<milliseconds>(concurrent_2 - concurrent_1).count()) / float(1000) << "s" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment