include pthread include stdio int sum0 sum1 sum2 int array0 int array1

 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <pthread.h>
#include <stdio.h>
int sum0 = 0, sum1 = 0, sum2 = 0;
int array0[] = {1, 2, 3, 4, 5};
int array1[] = {1, 4, 3, 4, 6};
int array2[] = {2, 5, 6, 7, 9};
pthread_barrier_t barr;
pthread_t threads[3];
void *func(void *p)
{
while (1) {
for (int i = 0; i < 5; i++)
{
if ((int)p == 0) {
sum0 += array0[i];
}
if ((int)p == 1) {
sum1 += array1[i];
}
if ((int)p == 2) {
sum2 += array2[i];
}
}
if ((int)p == 0)
printf("Сумма чисел первого массива: %d\n", sum0);
if ((int)p == 1)
printf("Сумма чисел второго массива: %d\n", sum1);
if ((int)p == 2)
printf("Сумма чисел третьего массива: %d\n", sum2);
pthread_barrier_wait(&barr);
if (sum0 == sum1 && sum1 == sum2 && sum0 == sum2)
break;
if (sum0 > sum1) {
array0[3]--;
sum0 = 0;
sum1 = 0;
sum2 = 0;
}
if (sum0 < sum1) {
array0[0]++;
sum0 = 0;
sum1 = 0;
sum2 = 0;
}
if (sum2 > sum1) {
array2[3]--;
sum0 = 0;
sum1 = 0;
sum2 = 0;
}
if (sum2 < sum1) {
array2[0]++;
sum0 = 0;
sum1 = 0;
sum2 = 0;
}
pthread_barrier_wait(&barr);
}
return NULL;
}
int main()
{
pthread_barrier_init(&barr, NULL, 2);
for (int i = 0; i < 3; i++)
pthread_create(&threads[i], NULL, func, (void *)i);
for (int i = 0; i < 3; i++)
pthread_join(threads[i], NULL);
return 0;
}