-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (33 loc) · 705 Bytes
/
Copy pathmain.cpp
File metadata and controls
40 lines (33 loc) · 705 Bytes
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
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
std::mutex mtx;
int counter = 0;
void increment() {
for (size_t i = 0; i < 10000; i++) {
// mtx.lock();
counter++;
// mtx.unlock();
}
}
int main() {
// example 1 - multiple lines of assembly
std::thread t1(increment);
std::thread t2(increment);
// t1.join();
// t2.join();
// cout << "counter: " << counter << endl;
// example 2 - 1 line of assembly
for (int i = 0; i < 10000; i++) {
counter = 0;
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
if (counter < 20000) {
cout << "counter: " << counter << endl;
}
}
return 0;
}