1 /* Making a library function that uses static variables thread-safe.
2 Illustrates: thread-specific data, pthread_once(). */
10 /* This is a typical example of a library function that uses
11 static variables to accumulate results between calls.
12 Here, it just returns the concatenation of all string arguments
13 that were given to it. */
17 char * str_accumulate(char * s
)
19 static char accu
[1024] = { 0 };
26 /* Of course, this cannot be used in a multi-threaded program
27 because all threads store "accu" at the same location.
28 So, we'll use thread-specific data to have a different "accu"
31 /* Key identifying the thread-specific data */
32 static pthread_key_t str_key
;
33 /* "Once" variable ensuring that the key for str_alloc will be allocated
35 static pthread_once_t str_alloc_key_once
= PTHREAD_ONCE_INIT
;
37 /* Forward functions */
38 static void str_alloc_key(void);
39 static void str_alloc_destroy_accu(void * accu
);
41 /* Thread-safe version of str_accumulate */
43 char * str_accumulate(const char * s
)
47 /* Make sure the key is allocated */
48 pthread_once(&str_alloc_key_once
, str_alloc_key
);
49 /* Get the thread-specific data associated with the key */
50 accu
= (char *) pthread_getspecific(str_key
);
51 /* It's initially NULL, meaning that we must allocate the buffer first. */
54 if (accu
== NULL
) return NULL
;
56 /* Store the buffer pointer in the thread-specific data. */
57 pthread_setspecific(str_key
, (void *) accu
);
58 printf("Thread %lx: allocating buffer at %p\n", pthread_self(), accu
);
60 /* Now we can use accu just as in the non thread-safe code. */
65 /* Function to allocate the key for str_alloc thread-specific data. */
67 static void str_alloc_key(void)
69 pthread_key_create(&str_key
, str_alloc_destroy_accu
);
70 printf("Thread %lx: allocated key %d\n", pthread_self(), str_key
);
73 /* Function to free the buffer when the thread exits. */
74 /* Called only when the thread-specific data is not NULL. */
76 static void str_alloc_destroy_accu(void * accu
)
78 printf("Thread %lx: freeing buffer at %p\n", pthread_self(), accu
);
84 void * process(void * arg
)
87 res
= str_accumulate("Result of ");
88 res
= str_accumulate((char *) arg
);
89 res
= str_accumulate(" thread");
90 printf("Thread %lx: \"%s\"\n", pthread_self(), res
);
94 int main(int argc
, char ** argv
)
99 res
= str_accumulate("Result of ");
100 pthread_create(&th1
, NULL
, process
, (void *) "first");
101 pthread_create(&th2
, NULL
, process
, (void *) "second");
102 res
= str_accumulate("initial thread");
103 printf("Thread %lx: \"%s\"\n", pthread_self(), res
);
104 pthread_join(th1
, NULL
);
105 pthread_join(th2
, NULL
);