-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-7thread.c
53 lines (45 loc) · 1014 Bytes
/
4-7thread.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
#include <stdio.h>
#include <pthread.h>
#define errlog(errmsg) do{perror(errmsg);\
printf("--%s--%s--%d--\n",\
__FILE__, __FUNCTION__, __LINE__);\
return -1;\
}while(0)
int value1 = 0;
int value2 = 0;
int count = 0;
void *thread1_handler(void *arg){
while(1){
value1 = count;
value2 = count;
count++;
}
pthread_exit("thread1...exit");
}
void *thread2_handler(void *arg){
while(1){
if(value1 != value2){
sleep(1);
printf("value1 = %d value2 = %d\n", value1, value2);
}
}
pthread_exit("thread2...exit");
}
int main(int argc, const char *argv[])
{
pthread_t thread1, thread2;
void *retval;
if(pthread_create(&thread1, NULL,
thread1_handler, NULL) != 0){
errlog("pthread_create1 error");
}
if(pthread_create(&thread2, NULL,
thread2_handler, NULL) != 0){
errlog("pthread_create2 error");
}
pthread_join(thread1, &retval);
printf("%s\n", (char *)retval);
pthread_join(thread2, &retval);
printf("%s\n", (char *)retval);
return 0;
}