-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOM_RC.cpp
63 lines (46 loc) · 996 Bytes
/
COM_RC.cpp
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
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <vector>
#include <mutex> // std::mutex
std::mutex mtx;
long long int SALDO = 0;
using namespace std;
void increment(int arg)
{
int i;
for (i = 1; i <= 10000 ; i++)
{
mtx.lock();
SALDO = SALDO + 1;
mtx.unlock();
printf("incremento => <thread %d>: %lld\n", arg, SALDO);
}
}
void decrement(int arg)
{
int i;
for (i = 1; i <= 10000 ; i++)
{
mtx.lock();
SALDO = SALDO - 1;
mtx.unlock();
printf("desc => <thread %d>: %lld\n", arg, SALDO);
}
}
int main()
{
int n=22;
std::vector<thread> threads(n);
for (int i = 0; i < n; i+=2) {
threads[i] = thread(increment, i + 1);
threads[i+1] = thread(decrement, i + 2);
}
for (auto& th : threads) {
th.join();
}
printf("O Saldo final %lld\n",SALDO);
return 0;
}