ex4.c 3.2 KB

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