加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_丽江站长网 (http://www.0888zz.com/)- 科技、建站、数据工具、云上网络、机器学习!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

UNIX环境高级编程:线程私有数据

发布时间:2016-09-27 17:04:36 所属栏目:Unix 来源:站长网
导读:副标题#e# 线程私有数据(Thread-specific data,TSD):存储和查询与某个线程相关数据的一种机制。 在进程内的所有线程都共享相同的地址空间,即意味着任何声明为静态或外部变量,或在进程堆声明的变量,都可以被进程内所有的线程读写。 一个线程真正拥有的

示例代码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);

}

(编辑:应用网_丽江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读