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

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

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

注意:两个线程对自己的私有数据操作是互相不影响的。也就是说,虽然 key 是同名且全局,但访问的内存空间并不是相同的一个。key 就像是一个数据管理员,线程的私有数据只是到他那去注册,让它知道你这个数据的存在。

示例代码:

[cpp] view plaincopyprint

01.#include <stdio.h>

02.#include <pthread.h>

03.#include <stdlib.h>

04.

05.typedef struct private_tag {

06. pthread_t thread_id;

07. char *string;

08.} private_t;

09.

10.pthread_key_t identity_key; /* Thread-specific data key */

11.pthread_mutex_t identity_key_mutex = PTHREAD_MUTEX_INITIALIZER;

12.long identity_key_counter = 0;

13.

14.

15.void identity_key_destructor (void *value)

16.{

17. private_t *private = (private_t*)value;

18. int status;

19.

20. printf ("thread "%s" exiting...n", private->string);

21. free (value);

22. status = pthread_mutex_lock (&identity_key_mutex);

23. if (status != 0)

24. perror("pthread_mutex_lock");

25. identity_key_counter--;

26. if (identity_key_counter <= 0) {

27. status = pthread_key_delete (identity_key);

28. if (status != 0)

29. perror("pthread_key_delete");

30. printf ("key deleted...n");

31. }

32. status = pthread_mutex_unlock (&identity_key_mutex);

33. if (status != 0)

34. perror("pthread_mutex_unlock");

35.}

36.

37.void *identity_key_get (void)

38.{

39. void *value;

40. int status;

41.

42. value = pthread_getspecific (identity_key);

43. if (value == NULL) {

44. value = malloc (sizeof (private_t));

45. if (value == NULL)

46. perror ("malloc");

47. status = pthread_setspecific (identity_key, (void*)value);

48. if (status != 0)

49. perror("pthread_setspecific");

50. }

51. return value;

52.}

53.

54.void *thread_routine (void *arg)

55.{

56. private_t *value;

57.

58. value = (private_t*)identity_key_get ();

59. value->thread_id = pthread_self ();

60. value->string = (char*)arg;

61. printf ("thread "%s" starting...n", value->string);

62. sleep (2);

63. return NULL;

64.}

65.

66.void main (int argc, char *argv[])

67.{

68. pthread_t thread_1, thread_2;

69. private_t *value;

70. int status;

71.

72. status = pthread_key_create (&identity_key, identity_key_destructor);

73. if (status != 0)

74. perror("pthread_key_create");

75. identity_key_counter = 3;

76. value = (private_t*)identity_key_get ();

77. value->thread_id = pthread_self ();

78. value->string = "Main thread";

79. status = pthread_create (&thread_1, NULL,thread_routine, "Thread 1");

80. if (status != 0)

81. perror("pthread_create");

82. status = pthread_create (&thread_2, NULL,thread_routine, "Thread 2");

83. if (status != 0)

84. perror("pthread_create");

85. pthread_exit (NULL);

86.}

#include <stdio.h>

#include <pthread.h>

#include <stdlib.h>

typedef struct private_tag {

pthread_t thread_id;

char *string;

} private_t;

pthread_key_t identity_key; /* Thread-specific data key */

pthread_mutex_t identity_key_mutex = PTHREAD_MUTEX_INITIALIZER;

long identity_key_counter = 0;

void identity_key_destructor (void *value)

{

private_t *private = (private_t*)value;

int status;

printf ("thread "%s" exiting...n", private->string);

free (value);

status = pthread_mutex_lock (&identity_key_mutex);

if (status != 0)

perror("pthread_mutex_lock");

identity_key_counter--;

if (identity_key_counter <= 0) {

status = pthread_key_delete (identity_key);

if (status != 0)

perror("pthread_key_delete");

printf ("key deleted...n");

}

status = pthread_mutex_unlock (&identity_key_mutex);

if (status != 0)

perror("pthread_mutex_unlock");

}

void *identity_key_get (void)

{

void *value;

int status;

value = pthread_getspecific (identity_key);

if (value == NULL) {

value = malloc (sizeof (private_t));

if (value == NULL)

perror ("malloc");

status = pthread_setspecific (identity_key, (void*)value);

if (status != 0)

perror("pthread_setspecific");

}

return value;

}

void *thread_routine (void *arg)

{

private_t *value;

value = (private_t*)identity_key_get ();

value->thread_id = pthread_self ();

value->string = (char*)arg;

printf ("thread "%s" starting...n", value->string);

sleep (2);

return NULL;

}

void main (int argc, char *argv[])

{

pthread_t thread_1, thread_2;

private_t *value;

int status;

status = pthread_key_create (&identity_key, identity_key_destructor);

if (status != 0)

perror("pthread_key_create");

identity_key_counter = 3;

value = (private_t*)identity_key_get ();

value->thread_id = pthread_self ();

value->string = "Main thread";

status = pthread_create (&thread_1, NULL,thread_routine, "Thread 1");

if (status != 0)

perror("pthread_create");

status = pthread_create (&thread_2, NULL,thread_routine, "Thread 2");

if (status != 0)

perror("pthread_create");

pthread_exit (NULL);

查看本栏目更多精彩内容:http://www.bianceng.cn/OS/unix/

}运行结果:

[cpp] view plaincopyprint

01.huangcheng@ubuntu:~$ ./a.out

02.thread "Main thread" exiting...

03.thread "Thread 2" starting...

04.thread "Thread 1" starting...

05.thread "Thread 2" exiting...

06.thread "Thread 1" exiting...

07.key deleted...

08.huangcheng@ubuntu:~$

huangcheng@ubuntu:~$ ./a.out

thread "Main thread" exiting...

thread "Thread 2" starting...

thread "Thread 1" starting...

thread "Thread 2" exiting...

thread "Thread 1" exiting...

key deleted...

huangcheng@ubuntu:~$

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

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

推荐文章
    热点阅读