- 论坛徽章:
- 0
|
原帖由 chenzhanyiczy 于 2007-5-25 15:07 发表
关于pthread_key,有一些问题,下面用例子来说明:
pthread_key_t p_key;
char test_key_val[30];
main
{
pthread_key_create(&p_key,NULL);
pthread_create(&p_thread_id[1],NULL,a ...
楼主下次再贴代码时,最好把你的代码贴全,方便别人帮你
不知道楼主怎么能将你的代码编译通过的,还有一些语法错误
下面是我根据你的代码修改的
- #include <pthread.h>
- pthread_key_t p_key;
- char test_key_val[30];
- a()
- {
- strcpy(test_key_val,"I am a");
- pthread_setspecific(p_key,test_key_val);
- printf("Test_key_val is [%s] in a function\n",test_key_val);
- }
- b()
- {
- strcpy(test_key_val,"I am b");
- pthread_setspecific(p_key,test_key_val);
- printf("Test_key_val is [%s] in b function\n",test_key_val);
- }
- int main()
- {
- pthread_t p1, p2;
-
- pthread_key_create(&p_key,NULL);
-
- pthread_create(&p1,NULL,a,NULL);
- pthread_create(&p2,NULL,b,NULL);
-
- pthread_join(p1, NULL);
- pthread_join(p2,NULL);
- }
复制代码
运行结果是:
Test_key_val is [I am a] in a function
Test_key_val is [I am b] in b function
跟你的代码基本上没有区别 |
|