首页 > 编程语言 > 详细

线程私有数据和pthread_once

时间:2016-05-13 07:28:57      阅读:185      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
#include <pthread.h>

pthread_key_t key;
pthread_once_t ponce = PTHREAD_ONCE_INIT;

void ronce(){
        printf("%s\n", "ronce");
}

void *thread1(){
        pthread_setspecific(key, "thread1");
        printf("%s\n", pthread_getspecific(key));

        pthread_once(&ponce, ronce);
}

void *thread2(){
        pthread_setspecific(key, "thread2");
        printf("%s\n", pthread_getspecific(key));

        pthread_once(&ponce, ronce);
}

int main(){
        pthread_key_create(&key, NULL);
        pthread_t tid1, tid2;
        pthread_create(&tid1, NULL, thread1, NULL);
        pthread_create(&tid2, NULL, thread2, NULL);

        pthread_join(tid1, NULL);
        pthread_join(tid2, NULL);
        pthread_key_delete(key);
}

 

关于线程私有数据:http://blog.csdn.net/cywosp/article/details/26469435

关于pthread_once:http://blog.csdn.net/lmh12506/article/details/8452659

 

线程私有数据和pthread_once

原文:http://www.cnblogs.com/bai-jimmy/p/5484791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!