免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5855 | 回复: 6
打印 上一主题 下一主题

[网络] 关于线程特定数据共享全局变量的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-10-05 13:18 |只看该作者 |倒序浏览
我有一个问题,线程特定数据怎么理解,我看了些小程序,差不多是不是这个意思,进程有一个key,然后每个线程给进程的这个key设定一个值(指针),各个线程的值是独立的,于是我看了下下面这个程序,表示不淡定了,共享全局变量的问题。
#include <stdio.h>
#include <pthread.h>

int my_errno = 0;
pthread_key_t key;

void *thread2(void *arg)
{
        my_errno = 2;
        pthread_setspecific(key, &my_errno);
       
        printf("thread2: %u; pkey address: %p; pkey value: %d, my_errno = %d, my_errno address = %p\n",
                (unsigned int)pthread_self(), (int *)pthread_getspecific(key),
                *((int *)pthread_getspecific(key)), my_errno, &my_errno);
}

void *thread1(void *arg)
{
        my_errno = 1;
    pthread_setspecific(key, &my_errno);

        printf("thread2: %u; pkey address: %p; pkey value: %d, my_errno = %d, my_errno address = %p\n",
                (unsigned int)pthread_self(), (int *)pthread_getspecific(key),
                *((int *)pthread_getspecific(key)), my_errno, &my_errno);
}

int main(void)
{
    pthread_t thid1, thid2;

    printf("main thread begins running, my_errno=%d, my_errno address = %p\n",my_errno, &my_errno);
    pthread_key_create(&key, NULL);
       
    pthread_create(&thid1, NULL, thread1, NULL);
        pthread_create(&thid2, NULL, thread2, NULL);
        sleep(2);
    pthread_key_delete(key);
    printf("main thread exit\n");
    return 0;
}
程序输出:

main thread begins running, my_errno=0, my_errno address = 0x8049adc
thread2: 3067120496; pkey address: 0x8049adc; pkey value: 2, my_errno = 2, my_errno address = 0x8049adc
thread2: 3077610352; pkey address: 0x8049adc; pkey value: 1, my_errno = 1, my_errno address = 0x8049adc
main thread exit

我发现共享的全局变量的地址是一样的 0x8049b0c,但是值不一样,既然是特定数据,我的理解是每个线程为此数据单独开辟一个空间存储,相当于存储副本,但是事实好像不是这样子,麻烦高手帮忙解释一下吧。

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
2 [报告]
发表于 2012-10-05 14:58 |只看该作者
是第一个线程执行完退出了, 第二个线程才执行的吧.

楼主想多了.

论坛徽章:
0
3 [报告]
发表于 2012-10-05 15:30 |只看该作者
{:3_198:} {:3_198:} {:3_198:}
回复 2# linux_c_py_php


   

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
4 [报告]
发表于 2012-10-05 15:49 |只看该作者
汗, 你也在.

fdl19881 发表于 2012-10-05 15:30
回复 2# linux_c_py_php

论坛徽章:
0
5 [报告]
发表于 2012-10-05 15:53 |只看该作者
回复 4# linux_c_py_php

其实我已经有几个月没来CU了,,看你在csdn那说了,才来围观下哈,,,
   

论坛徽章:
4
水瓶座
日期:2013-09-06 12:27:30摩羯座
日期:2013-09-28 14:07:46处女座
日期:2013-10-24 14:25:01酉鸡
日期:2014-04-07 11:54:15
6 [报告]
发表于 2012-10-05 16:00 |只看该作者
我才来几周, 不活跃没东西学啊.

fdl19881 发表于 2012-10-05 15:53
回复 4# linux_c_py_php

其实我已经有几个月没来CU了,,看你在csdn那说了,才来围观下哈,,,

论坛徽章:
0
7 [报告]
发表于 2012-12-28 11:20 |只看该作者
线程私有数据??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP