-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutex.cpp
More file actions
55 lines (50 loc) · 1.31 KB
/
Copy pathFutex.cpp
File metadata and controls
55 lines (50 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdlib>
#include <thread>
#include <vector>
#include <algorithm>
#include <atomic>
#include <assert.h>
#include <mutex>
#include "Futex.h"
FuFutex mutex;
//std::mutex mutex;
std::vector<int> threads_rates(4);
void increment(volatile int& var, const int thread_index) {
while(true) {
mutex.lock();
if (var == 100000000) {
mutex.unlock();
break;
}
++threads_rates[thread_index];
++var;
mutex.unlock();
}
}
int main ()
{
std::chrono::time_point<std::chrono::system_clock> start, end;
int countOfThreads = std::thread::hardware_concurrency();
std::cout << "CountOfThreads could be " << countOfThreads <<
std::endl;
std::vector <std::thread> threads;
threads_rates.resize(countOfThreads);
start = std::chrono::system_clock::now();
volatile int val = 0;
for (int i = 0; i < countOfThreads; ++i) {
threads.push_back(std::thread(increment, std::ref(val), i));
}
for (int i = 0; i < countOfThreads; ++i) {
threads[i].join();
}
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::cout << elapsed_seconds.count() << std::endl;
std::cout << val << std::endl;
for (int i = 0 ;i < countOfThreads; ++i ) {
std::cout << threads_rates[i] << ' ';
}
//std::cout << *std::max_element(vector.begin(), vector.end());
return 0;
}