Back to index

glibc  2.9
ex4.c
Go to the documentation of this file.
00001 /* Making a library function that uses static variables thread-safe.
00002    Illustrates: thread-specific data, pthread_once(). */
00003 
00004 #include <stddef.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 #include <pthread.h>
00009 
00010 /* This is a typical example of a library function that uses
00011    static variables to accumulate results between calls.
00012    Here, it just returns the concatenation of all string arguments
00013    that were given to it. */
00014 
00015 #if 0
00016 
00017 char *
00018 str_accumulate (char *s)
00019 {
00020   static char accu[1024] = { 0 };
00021   strcat (accu, s);
00022   return accu;
00023 }
00024 
00025 #endif
00026 
00027 /* Of course, this cannot be used in a multi-threaded program
00028    because all threads store "accu" at the same location.
00029    So, we'll use thread-specific data to have a different "accu"
00030    for each thread. */
00031 
00032 /* Key identifying the thread-specific data */
00033 static pthread_key_t str_key;
00034 /* "Once" variable ensuring that the key for str_alloc will be allocated
00035    exactly once. */
00036 static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
00037 
00038 /* Forward functions */
00039 static void str_alloc_key (void);
00040 static void str_alloc_destroy_accu (void *accu);
00041 
00042 /* Thread-safe version of str_accumulate */
00043 
00044 static char *
00045 str_accumulate (const char *s)
00046 {
00047   char *accu;
00048 
00049   /* Make sure the key is allocated */
00050   pthread_once (&str_alloc_key_once, str_alloc_key);
00051   /* Get the thread-specific data associated with the key */
00052   accu = (char *) pthread_getspecific (str_key);
00053   /* It's initially NULL, meaning that we must allocate the buffer first. */
00054   if (accu == NULL)
00055     {
00056       accu = malloc (1024);
00057       if (accu == NULL)
00058        return NULL;
00059       accu[0] = 0;
00060       /* Store the buffer pointer in the thread-specific data. */
00061       pthread_setspecific (str_key, (void *) accu);
00062       printf ("Thread %lx: allocating buffer at %p\n", pthread_self (), accu);
00063     }
00064   /* Now we can use accu just as in the non thread-safe code. */
00065   strcat (accu, s);
00066   return accu;
00067 }
00068 
00069 /* Function to allocate the key for str_alloc thread-specific data. */
00070 
00071 static void
00072 str_alloc_key (void)
00073 {
00074   pthread_key_create (&str_key, str_alloc_destroy_accu);
00075   printf ("Thread %lx: allocated key %d\n", pthread_self (), str_key);
00076 }
00077 
00078 /* Function to free the buffer when the thread exits. */
00079 /* Called only when the thread-specific data is not NULL. */
00080 
00081 static void
00082 str_alloc_destroy_accu (void *accu)
00083 {
00084   printf ("Thread %lx: freeing buffer at %p\n", pthread_self (), accu);
00085   free (accu);
00086 }
00087 
00088 /* Test program */
00089 
00090 static void *
00091 process (void *arg)
00092 {
00093   char *res;
00094   res = str_accumulate ("Result of ");
00095   res = str_accumulate ((char *) arg);
00096   res = str_accumulate (" thread");
00097   printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
00098   return NULL;
00099 }
00100 
00101 int
00102 main (int argc, char **argv)
00103 {
00104   char *res;
00105   pthread_t th1, th2;
00106 
00107   res = str_accumulate ("Result of ");
00108   pthread_create (&th1, NULL, process, (void *) "first");
00109   pthread_create (&th2, NULL, process, (void *) "second");
00110   res = str_accumulate ("initial thread");
00111   printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
00112   pthread_join (th1, NULL);
00113   pthread_join (th2, NULL);
00114   return 0;
00115 }