#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_key_t key;
struct test_struct {
int i;
float k;
};
void *child1 (void *arg)
{
struct test_struct struct_data;
struct_data.i = 10;
struct_data.k = 3.1415;
pthread_setspecific (key, &struct_data);
printf ("结构体struct_data的地址为 0x%p\n", &(struct_data));
printf ("child1 中 pthread_getspecific(key)返回的指针为:0x%p\n", (struct test_struct *)pthread_getspecific(key));
printf ("利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:\nstruct_data.i:%d\nstruct_data.k: %f\n", ((struct test_struct *)pthread_getspecific (key))->i, ((struct test_struct *)pthread_getspecific(key))->k);
printf ("------------------------------------------------------\n");
}
void *child2 (void *arg)
{
int temp = 20;
sleep (2);
printf ("child2 中变量 temp 的地址为 0x%p\n", &temp);
pthread_setspecific (key, &temp);
printf ("child2 中 pthread_getspecific(key)返回的指针为:0x%p\n", (int *)pthread_getspecific(key));
printf ("利用 pthread_getspecific(key)打印 child2 线程中与key关联的整型变量temp 值:%d\n", *((int *)pthread_getspecific(key)));
}
int main (void)
{
pthread_t tid1, tid2;
pthread_key_create (&key, NULL);
pthread_create (&tid1, NULL, (void *)child1, NULL);
pthread_create (&tid2, NULL, (void *)child2, NULL);
pthread_join (tid1, NULL);
pthread_join (tid2, NULL);
pthread_key_delete (key);
return (0);
}
./pthread_key
结构体struct_data的地址为 0x0xb7699388
child1 中 pthread_getspecific(key)返回的指针为:0x0xb7699388
利用 pthread_getspecific(key)打印 child1 线程中与key关联的结构体中成员值:
struct_data.i:10
struct_data.k: 3.141500
------------------------------------------------------
child2 中变量 temp 的地址为 0x0xb6e9838c
child2 中 pthread_getspecific(key)返回的指针为:0x0xb6e9838c
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
pthread_key_t thread_log_key;
/*通用函数里可以利用 pthread_getspecific() 处理线程各自的私有数据*/
void write_to_thread_log (const char *message)
{
FILE *thread_log = (FILE *)pthread_getspecific (thread_log_key);
fprintf (thread_log, "%s\\n", message);
}
void close_thread_log (void *thread_log)
{
fclose ((FILE *)thread_log);
}
void *thread_function (void *args)
{
char thread_log_filename[128];
char thread_start_message[128];
FILE *thread_log;
sprintf (thread_log_filename, "thread%u.log", pthread_self());
thread_log = fopen (thread_log_filename, "w");
pthread_setspecific (thread_log_key, thread_log); //每个线程都设置自己的私有数据
sprintf (thread_start_message, "thread %u starting", pthread_self());
write_to_thread_log (thread_start_message);
pthread_exit(NULL);
}
int main()
{
int i;
pthread_t threads[5];
/*创建私有数据键,close_thread_log 在线程退出时对 key 关联数据进行清理*/
pthread_key_create (&thread_log_key, close_thread_log);
for (i = 0; i < 5; i++)
pthread_create (&threads[i], NULL, thread_function, NULL); //创建多线程
for (i = 0; i < 5; i++)
pthread_join (threads[i], NULL); //等待各个线程结束
return (0);
}
./pthread_key2
ls *log
thread3040865136.log thread3049257840.log thread3059047280.log thread3067439984.log thread3075832688.log
cat thread3040865136.log
thread 3040865136 starting
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |