-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread4.c
53 lines (45 loc) · 820 Bytes
/
thread4.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREAD 100
void *thread_inc(void*arg);
void *thread_des(void*arg);
long long num=0;
int main(int argc, char const *argv[])
{
pthread_t thread_id[NUM_THREAD];
int i;
printf("size of long long : %ld\n", sizeof(long long));
for (i = 0; i < NUM_THREAD; ++i)
{
if(i%2)
pthread_create(&thread_id[i],NULL, thread_inc, NULL);
else
pthread_create(&thread_id[i],NULL, thread_des, NULL);
}
for (i = 0; i < NUM_THREAD; ++i)
{
pthread_join(thread_id[i],NULL);
}
printf("result: %lld\n", num);
return 0;
}
void *thread_inc(void *arg)
{
int i;
for (i = 0; i < 50000000; ++i)
{
num+=1;
}
return NULL;
}
void *thread_des(void *arg)
{
int i;
for (i = 0; i < 50000000; ++i)
{
num-=1;
}
return NULL;
}