码迷,mamicode.com
首页 > 编程语言 > 详细

线程私有数据和pthread_once

时间:2016-05-13 07:29:12      阅读:255      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!