-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrace.cpp
More file actions
45 lines (31 loc) · 750 Bytes
/
race.cpp
File metadata and controls
45 lines (31 loc) · 750 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
41
42
43
44
#include <iostream>
#include <pthread.h>
int x;
int *z;
void* worker(void* unused) {
*z = 20;
x = rand() + (*z);
std::cout << x << "\n";
return nullptr;
}
void* worker2(void* unused) {
int k = rand();
std::cout << k << "\n";
return nullptr;
}
int main() {
int unused;
pthread_t th1, th2;
pthread_create(&th1, nullptr, worker, nullptr);
pthread_create(&th2, nullptr, worker, nullptr);
pthread_join(th1, nullptr);
pthread_join(th2, nullptr);
int i;
int j = i + 4;
std::cout << j << "\n";
pthread_create(&th1, nullptr, worker2, nullptr);
pthread_create(&th2, nullptr, worker2, nullptr);
pthread_join(th1, nullptr);
pthread_join(th2, nullptr);
return 0;
}