-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
37 lines (29 loc) · 767 Bytes
/
main.cpp
File metadata and controls
37 lines (29 loc) · 767 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
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
using namespace std;
extern "C" void Lock(int32_t *);
extern "C" void Unlock(int32_t *);
int32_t lock_variable = 0;
const uint32_t NUM_THREADS = 16;
void Worker(int32_t id) {
int32_t counter = 0;
while (counter < 4) {
Lock(&lock_variable);
counter++;
cout << "thread: " << id << " counter: " << counter << endl;
std::this_thread::sleep_for(chrono::milliseconds(5));
Unlock(&lock_variable);
sched_yield();
}
}
int main() {
vector<thread *> threads;
for (uint32_t i = 0; i < NUM_THREADS; i++) {
threads.push_back(new thread(Worker, i));
}
for (auto & t : threads)
t->join();
return 0;
}