ex4.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Making a library function that uses static variables thread-safe.
  2. Illustrates: thread-specific data, pthread_once(). */
  3. #include <stddef.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. /* This is a typical example of a library function that uses
  9. static variables to accumulate results between calls.
  10. Here, it just returns the concatenation of all string arguments
  11. that were given to it. */
  12. #if 0
  13. static char * str_accumulate(char * s)
  14. {
  15. static char accu[1024] = { 0 };
  16. strcat(accu, s);
  17. return accu;
  18. }
  19. #endif
  20. /* Of course, this cannot be used in a multi-threaded program
  21. because all threads store "accu" at the same location.
  22. So, we'll use thread-specific data to have a different "accu"
  23. for each thread. */
  24. /* Key identifying the thread-specific data */
  25. static pthread_key_t str_key;
  26. /* "Once" variable ensuring that the key for str_alloc will be allocated
  27. exactly once. */
  28. static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
  29. /* Forward functions */
  30. static void str_alloc_key(void);
  31. static void str_alloc_destroy_accu(void * accu);
  32. /* Thread-safe version of str_accumulate */
  33. static char * str_accumulate(const char * s)
  34. {
  35. char * accu;
  36. /* Make sure the key is allocated */
  37. pthread_once(&str_alloc_key_once, str_alloc_key);
  38. /* Get the thread-specific data associated with the key */
  39. accu = (char *) pthread_getspecific(str_key);
  40. /* It's initially NULL, meaning that we must allocate the buffer first. */
  41. if (accu == NULL) {
  42. accu = malloc(1024);
  43. if (accu == NULL) return NULL;
  44. accu[0] = 0;
  45. /* Store the buffer pointer in the thread-specific data. */
  46. pthread_setspecific(str_key, (void *) accu);
  47. printf("Thread %lx: allocating buffer at %p\n", pthread_self(), accu);
  48. }
  49. /* Now we can use accu just as in the non thread-safe code. */
  50. strcat(accu, s);
  51. return accu;
  52. }
  53. /* Function to allocate the key for str_alloc thread-specific data. */
  54. static void str_alloc_key(void)
  55. {
  56. pthread_key_create(&str_key, str_alloc_destroy_accu);
  57. printf("Thread %lx: allocated key %d\n", pthread_self(), str_key);
  58. }
  59. /* Function to free the buffer when the thread exits. */
  60. /* Called only when the thread-specific data is not NULL. */
  61. static void str_alloc_destroy_accu(void * accu)
  62. {
  63. printf("Thread %lx: freeing buffer at %p\n", pthread_self(), accu);
  64. free(accu);
  65. }
  66. /* Test program */
  67. static void * process(void * arg)
  68. {
  69. char * res;
  70. res = str_accumulate("Result of ");
  71. res = str_accumulate((char *) arg);
  72. res = str_accumulate(" thread");
  73. printf("Thread %lx: \"%s\"\n", pthread_self(), res);
  74. return NULL;
  75. }
  76. int main(int argc, char ** argv)
  77. {
  78. char * res;
  79. pthread_t th1, th2;
  80. res = str_accumulate("Result of ");
  81. pthread_create(&th1, NULL, process, (void *) "first");
  82. pthread_create(&th2, NULL, process, (void *) "second");
  83. res = str_accumulate("initial thread");
  84. printf("Thread %lx: \"%s\"\n", pthread_self(), res);
  85. pthread_join(th1, NULL);
  86. pthread_join(th2, NULL);
  87. exit(0);
  88. }