-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread-cp4.c
59 lines (46 loc) · 937 Bytes
/
pthread-cp4.c
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
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
volatile long int a = 0;
void threadFun(void *arg)
{
int i;
long int localA =0;
for(i = 1; i < 500000; i++)
{
localA = localA + i;
}
pthread_mutex_lock(&a);
a = a + i;
pthread_mutex_unlock(&a);
}
void threadFun2(void *arg)
{
int i;
long int localA =0;
for(i = 500000; i < 1000000; i++)
{
localA = localA + i;
}
pthread_mutex_lock(&a);
a = a + i;
pthread_mutex_unlock(&a);
}
int main(int argc, char **argv)
{
pthread_t one, two;
int i;
a = 0;
/*
pthread_create(&one, NULL, (void*)&threadFun, NULL);
pthread_create(&two, NULL, (void*)&threadFun2, NULL);
pthread_join(one, NULL;
pthread_join(two, NULL);
*/
for (i = 1; i <= 1000000; i++)
{
a = a + i;
}
printf("%ld\n", a);
}