-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExerc3.cpp
61 lines (37 loc) · 931 Bytes
/
Exerc3.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 <iostream>
#include <pthread.h>
using namespace std;
pthread_t thread1, thread2;
pthread_mutex_t mutex;
int vetor[14]= {2423,4564,853,2243,65756,32,3455,244,346546,22,4564,2,435,457447};
int result=0;
void *part1 (void *arg){
int temp;
for(int i=0; i<7; i++){
pthread_mutex_lock(&mutex);
temp= result;
temp+=vetor[i];
result=temp;
pthread_mutex_unlock(&mutex);
}
pthread_exit(0);
}
void *part2 (void *arg){
int temp;
for(int i=7; i<14; i++){
pthread_mutex_lock(&mutex);
temp= result;
temp+=vetor[i];
result=temp;
pthread_mutex_unlock(&mutex);
}
pthread_exit(0);
}
int main (){
pthread_create(&thread1, NULL, part1, NULL);
pthread_create(&thread2, NULL, part2, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
cout<<"Result: "<<result;
return 0;
}