Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1021735
  • 博文数量: 297
  • 博客积分: 11721
  • 博客等级: 上将
  • 技术积分: 3431
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 10:21
文章分类

全部博文(297)

文章存档

2016年(9)

2011年(71)

2010年(137)

2009年(80)

分类: LINUX

2010-03-23 14:36:41

/*
    线程私有数据使用方法演示
*/

#include
#include
#include
#include

static pthread_key_t key_charsend, key_sendout;
static pthread_once_t init_done = PTHREAD_ONCE_INIT;


static void thread_key_init()
{
    pthread_key_create(&key_charsend, free);
}

void *
thr_fn(void *arg)
{
    pthread_once(&init_done, thread_key_init);
    char *char_send;
    char_send = (char *)malloc(50);
    pthread_setspecific(key_charsend,char_send);

    sprintf(char_send, "%lx", pthread_self());

    printf("pid = %lx pthread_getsecific(key_charsend) = %s\n",pthread_self(),(char *)pthread_getspecific(key_charsend));

    return((void *)0);
}

int
main(void)
{
    pthread_t tid1,tid2;
    pthread_create(&tid1, NULL, thr_fn, NULL);
    pthread_create(&tid2, NULL, thr_fn,NULL);
    
    return 0;
}
阅读(700) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~