-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-1.cpp
More file actions
54 lines (45 loc) · 778 Bytes
/
example-1.cpp
File metadata and controls
54 lines (45 loc) · 778 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
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include "mingw.thread.h"
#include "mingw.mutex.h"
using namespace std;
const unsigned int N = 256;
mutex g_lock;
void read(int *arr)
{
while (true)
{
for (int i = 0; i < N; i++)
{
g_lock.lock();
cout << arr[i] << endl;
g_lock.unlock();
}
}
}
void write(int *arr)
{
while (true)
{
for (int i = 0; i < N; i++)
{
g_lock.lock();
arr[i] += 1;
g_lock.unlock();
}
}
}
int main()
{
int *array = new int[N];
// Init
for (int i = 0; i < N; i++)
{
array[i] = 0;
}
// Run
thread t1(read, ref(array));
thread t2(write, ref(array));
t1.join();
t2.join();
return 0;
}