- 论坛徽章:
- 0
|
#include <pthread.h>
#include "errors.h"
typedef struct tsd_tag{
pthread_t thread_id ;
char *string ;
}tsd_t ;
pthread_key_t tsd_key ;
pthread_once_t key_once = PTHREAD_ONCE_INIT ;
void once_routine(void)
{
int status ;
printf("initializing key\n") ;
status = pthread_key_create(&tsd_key,NULL) ;
if(status != 0)
err_abort(status,"Create key") ;
}
void *thread_routine(void *arg)
{
tsd_t *value ;
int status ;
status = pthread_once(&key_once,once_routine) ;
if(status != 0)
err_abort(status,"once_init") ;
value = (tsd_t*)malloc(sizeof(tsd_t)) ;
if(value ==NULL)
errno_abort("allocate key value") ;
status = pthread_setspecific(tsd_key,value) ;
if(status != 0)
err_abort(status,"set tsd") ;
printf("%s set tsd value %p \n",arg,value) ;
value->thread_id = pthread_self() ;
value->string = (char *)arg ;
value = (tsd_t*)pthread_getspecific(tsd_key) ;
printf("%s starting...\n",value->string) ;
sleep(2) ;
value = (tsd_t*)pthread_getspecific(tsd_key) ;
printf("%s done...\n",value->string) ;
return NULL ;
}
void main(int argc,char *argv[])
{
pthread_t thread1,thread2 ;
int status ;
status = pthread_create(&thread1,NULL,thread_routine,"thread1") ;
if(status != 0)
err_abort(status,"Create thread1") ;
status = pthread_create(&thread2,NULL,thread_routine,"thread2") ;
if(status != 0)
err_abort(status,"Creat thread2") ;
pthread_exit(NULL) ;
} |
|