-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
44 lines (36 loc) · 839 Bytes
/
test.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
#include <stdio.h>
#include <pthread.h>
pthread_key_t pkey;
void function_(int num)
{
if (num == 100)
printf("it is in first thread\n");
else if (num == 200)
printf("it is in second thread\n");
}
void function(void)
{
int *tmp = (int *)pthread_getspecific(pkey);
printf("%d is runing in %s\n", *tmp, __FUNCTION__);
function_(*tmp);
}
void *p_function(void *arg)
{
pthread_setspecific(pkey, arg);
int *tmp = (int *)pthread_getspecific(pkey);
printf("%d is runing in %s\n", *tmp, __FUNCTION__);
*tmp = *tmp * 100;
function();
return (void*)0;
}
int main(void)
{
int a = 1, b = 2;
pthread_t tid, tid_;
pthread_key_create(&pkey, NULL);
pthread_create(&tid, NULL, p_function, &a);
pthread_create(&tid_, NULL, p_function, &b);
pthread_join(tid, NULL);
pthread_join(tid_, NULL);
return 0;
}