亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5983 | 回復(fù): 6
打印 上一主題 下一主題

[網(wǎng)絡(luò)] 關(guān)于線程特定數(shù)據(jù)共享全局變量的一個(gè)問題 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-10-05 13:18 |只看該作者 |倒序?yàn)g覽
我有一個(gè)問題,線程特定數(shù)據(jù)怎么理解,我看了些小程序,差不多是不是這個(gè)意思,進(jìn)程有一個(gè)key,然后每個(gè)線程給進(jìn)程的這個(gè)key設(shè)定一個(gè)值(指針),各個(gè)線程的值是獨(dú)立的,于是我看了下下面這個(gè)程序,表示不淡定了,共享全局變量的問題。
#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

我發(fā)現(xiàn)共享的全局變量的地址是一樣的 0x8049b0c,但是值不一樣,既然是特定數(shù)據(jù),我的理解是每個(gè)線程為此數(shù)據(jù)單獨(dú)開辟一個(gè)空間存儲(chǔ),相當(dāng)于存儲(chǔ)副本,但是事實(shí)好像不是這樣子,麻煩高手幫忙解釋一下吧。

論壇徽章:
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 [報(bào)告]
發(fā)表于 2012-10-05 14:58 |只看該作者
是第一個(gè)線程執(zhí)行完退出了, 第二個(gè)線程才執(zhí)行的吧.

樓主想多了.

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2012-10-05 15:30 |只看該作者
{:3_198:} {:3_198:} {:3_198:}
回復(fù) 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 [報(bào)告]
發(fā)表于 2012-10-05 15:49 |只看該作者
汗, 你也在.

fdl19881 發(fā)表于 2012-10-05 15:30
回復(fù) 2# linux_c_py_php

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2012-10-05 15:53 |只看該作者
回復(fù) 4# linux_c_py_php

其實(shí)我已經(jīng)有幾個(gè)月沒來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 [報(bào)告]
發(fā)表于 2012-10-05 16:00 |只看該作者
我才來幾周, 不活躍沒東西學(xué)啊.

fdl19881 發(fā)表于 2012-10-05 15:53
回復(fù) 4# linux_c_py_php

其實(shí)我已經(jīng)有幾個(gè)月沒來CU了,,看你在csdn那說了,才來圍觀下哈,,,

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2012-12-28 11:20 |只看該作者
線程私有數(shù)據(jù)??
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP