1 | |
---|
2 | /* test pthread_key_create/set_specific/get_specific |
---|
3 | get specific == set specific |
---|
4 | dtor is invoked (no mem leak) |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | extern "C"{ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <errno.h> |
---|
13 | #include <pthread.h> |
---|
14 | |
---|
15 | #define THREADS 5 |
---|
16 | #define BUFFSZ 48 |
---|
17 | pthread_key_t key; |
---|
18 | volatile int total_value,total_value_getspec; |
---|
19 | pthread_mutex_t value_mutex; |
---|
20 | |
---|
21 | void *threadfunc(void *parm) |
---|
22 | { |
---|
23 | int status; |
---|
24 | void *value; |
---|
25 | int threadnum; |
---|
26 | int *tnum; |
---|
27 | void *getvalue; |
---|
28 | char Buffer[BUFFSZ]; |
---|
29 | |
---|
30 | tnum = (int*)parm; |
---|
31 | threadnum = *tnum; |
---|
32 | |
---|
33 | //printf("Thread %d executing\n", threadnum); |
---|
34 | value = (void *)(rand()%100); |
---|
35 | status = pthread_setspecific(key, (void *) value); |
---|
36 | if ( status != 0) { |
---|
37 | printf("pthread_setspecific failed, thread %d, errno %d", |
---|
38 | threadnum, errno); |
---|
39 | return (void*)12; |
---|
40 | } |
---|
41 | pthread_mutex_lock(&value_mutex); |
---|
42 | total_value_getspec += (int)value; |
---|
43 | total_value += (int)pthread_getspecific(key); |
---|
44 | pthread_mutex_unlock(&value_mutex); |
---|
45 | |
---|
46 | |
---|
47 | if (!(value = malloc(sizeof(Buffer)))) |
---|
48 | printf("Thread %d could not allocate storage, errno = %d\n", |
---|
49 | threadnum, errno); |
---|
50 | status = pthread_setspecific(key, (void *) value); |
---|
51 | if ( status != 0) { |
---|
52 | printf("pthread_setspecific failed, thread %d, errno %d", |
---|
53 | threadnum, errno); |
---|
54 | return (void*)12; |
---|
55 | } |
---|
56 | //printf("Thread %d setspecific value: %d\n", threadnum, value); |
---|
57 | |
---|
58 | getvalue = 0; |
---|
59 | getvalue = pthread_getspecific(key); |
---|
60 | |
---|
61 | if (getvalue != value) { |
---|
62 | printf("getvalue not valid, getvalue=%d", (u_int64_t)getvalue); |
---|
63 | return (void*)68; |
---|
64 | } |
---|
65 | |
---|
66 | pthread_exit((void *)0); |
---|
67 | } |
---|
68 | |
---|
69 | void destr_fn(void *parm) |
---|
70 | { |
---|
71 | |
---|
72 | printf("Destructor function invoked\n"); |
---|
73 | free(parm); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | int main() { |
---|
78 | int getvalue; |
---|
79 | int status; |
---|
80 | int i; |
---|
81 | int threadparm[THREADS]; |
---|
82 | pthread_t threadid[THREADS]; |
---|
83 | void* thread_stat[THREADS]; |
---|
84 | |
---|
85 | // rand seed for testing |
---|
86 | srand(1003); |
---|
87 | pthread_mutex_init(&value_mutex, NULL); |
---|
88 | |
---|
89 | // testing getspec and setspec |
---|
90 | total_value = 0; |
---|
91 | total_value_getspec = 0; |
---|
92 | |
---|
93 | if ((status = pthread_key_create(&key, destr_fn )) < 0) { |
---|
94 | printf("pthread_key_create failed, errno=%d", errno); |
---|
95 | exit(1); |
---|
96 | } |
---|
97 | |
---|
98 | // create 3 THREADS, pass each its number |
---|
99 | for (i=0; i<THREADS; i++) { |
---|
100 | threadparm[i] = i+1; |
---|
101 | status = pthread_create( &threadid[i], |
---|
102 | NULL, |
---|
103 | threadfunc, |
---|
104 | (void *)&threadparm[i]); |
---|
105 | if ( status < 0) { |
---|
106 | printf("pthread_create failed, errno=%d", errno); |
---|
107 | exit(2); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | for ( i=0; i<THREADS; i++) { |
---|
112 | status = pthread_join( threadid[i], (void **)&thread_stat[i]); |
---|
113 | if ( status < 0) { |
---|
114 | printf("pthread_join failed, thread %d, errno=%d\n", i+1, errno); |
---|
115 | } |
---|
116 | |
---|
117 | if (thread_stat[i] != 0) { |
---|
118 | printf("bad thread status, thread %d, status=%d\n", i+1, |
---|
119 | (u_int64_t)thread_stat[i]); |
---|
120 | } |
---|
121 | } |
---|
122 | printf("total value is %d, total value by pthread_getspecific is %d\n", total_value, total_value_getspec); |
---|
123 | exit(0); |
---|
124 | } // main |
---|
125 | } |
---|
126 | |
---|