UNIX情形高级编程:线程私稀有据
示例代码2: [cpp] view plaincopyprint 01.#include <stdio.h> 02.#include <pthread.h> 03.#include <stdlib.h> 04. 05.typedef struct tsd_tag{ 06. pthread_t thread_id; 07. char *string; 08.}tsd_t; 09. 10.pthread_key_t key; 11.pthread_once_t once = PTHREAD_ONCE_INIT; 12. 13.void once_routine(void) 14.{ 15. int status; 16. 17. printf("Initializing keyn"); 18. status = pthread_key_create(&key, NULL); 19. if(status != 0){ 20. perror("pthread_key_create"); 21. } 22.} 23. 24.void *thread_routine(void *arg) 25.{ 26. int status; 27. tsd_t *value = NULL; 28. 29. status = pthread_once(&once, once_routine); 30. if(status != 0){ 31. perror("pthread_once"); 32. } 33. 34. value = (tsd_t *)malloc(sizeof(tsd_t)); 35. if(value == NULL){ 36. perror("malloc"); 37. } 38. 39. status = pthread_setspecific(key, (void *)value); 40. if(status != 0){ 41. perror("pthread_setspecific"); 42. } 43. 44. printf("%s set tsd value at %pn", (char *)arg, value); 45. value->thread_id = pthread_self(); 46. value->string = (char *)arg; 47. 48. printf("%s starting......n", (char *)arg); 49. sleep(2); 50. value = (tsd_t *)pthread_getspecific(key); 51. if(value == NULL){ 52. printf("no thread-specific data value was associated 53. with keyn"); 54. pthread_exit(NULL); 55. } 56. printf("%s done......n", value->string); 57.} 58. 59.int main(int argc, char **argv) 60.{ 61. pthread_t thread1, thread2; 62. int status; 63. 64. status = pthread_create(&thread1, NULL, thread_routine, "thread 1"); 65. if(status != 0){ 66. perror("pthread_create"); 67. } 68. 69. status = pthread_create(&thread2, NULL, thread_routine, "thread 2"); 70. if(status != 0){ 71. perror("pthread_create"); 72. } 73. 74. pthread_exit(NULL); 75.} #include <stdio.h> #include <pthread.h> #include <stdlib.h> typedef struct tsd_tag{ pthread_t thread_id; char *string; }tsd_t; pthread_key_t key; pthread_once_t once = PTHREAD_ONCE_INIT; void once_routine(void) { int status; printf("Initializing keyn"); status = pthread_key_create(&key, NULL); if(status != 0){ perror("pthread_key_create"); } } void *thread_routine(void *arg) { int status; tsd_t *value = NULL; status = pthread_once(&once, once_routine); if(status != 0){ perror("pthread_once"); } value = (tsd_t *)malloc(sizeof(tsd_t)); if(value == NULL){ perror("malloc"); } status = pthread_setspecific(key, (void *)value); if(status != 0){ perror("pthread_setspecific"); } printf("%s set tsd value at %pn", (char *)arg, value); value->thread_id = pthread_self(); value->string = (char *)arg; printf("%s starting......n", (char *)arg); sleep(2); value = (tsd_t *)pthread_getspecific(key); if(value == NULL){ printf("no thread-specific data value was associated with keyn"); pthread_exit(NULL); } printf("%s done......n", value->string); } int main(int argc, char **argv) { pthread_t thread1, thread2; int status; status = pthread_create(&thread1, NULL, thread_routine, "thread 1"); if(status != 0){ perror("pthread_create"); } status = pthread_create(&thread2, NULL, thread_routine, "thread 2"); if(status != 0){ perror("pthread_create"); } pthread_exit(NULL); } (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |