1,线程私有数据和线程之间的共享数据
线程私有:局部变量,函数的参数,线程局部存储(TLS),线程ID,当前指令指针(PC),寄存器集合。
线程之间共享:全局变量,堆上的数据,函数里静态变量,程序代码,打开的文件。
2,线程栈:
线程栈在不同的平台可能默认大小不一样,ARM平台一般默认大小为4M或者2M,可以通过ulimit命令查看,这个空间一般是普通线程的栈空间,主线程的空间要比这个大很多。
3,线程的私有数据:
点击打开链接线程要有私有数据的原因:1,有时候需要维护基于每个线程的数据,2,让基于进程的接口适应多线程环境,
线程私有数据的实现方式:
线程私有数据采用了一种一键多值的技术,即一个键对应多个数值。键可以被进程内的所有线程访问,但是每个
线程把这个键与不同的线程私有数据的地址关联。
int pthread_key_create(pthread_key_t *key, void (*destr_function) (void*));
int pthread_key_delete(pthread_key_t key);
键一旦创建,可以通过以下两个函数将键和线程的私有数据关联
pthread_setspecific:该函数将pointer的值(不是内容)与key相关联。用pthread_setspecific为一个键指定新的线程数据时,线程必须先释放原有的线程数据用以回收空间。
pthread_getspecific:通过该函数得到与key相关联的数据。
具体示例程序如下:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
int my_errno = 0;
pthread_key_t key;
void print_errno(char *str)
{
printf("%s my_errno:%d\n",str, my_errno);
}
void *thread2(void *arg)
{
printf("thread2 %ld is running\n",pthread_self());
pthread_setspecific(key, (void *)my_errno);
printf("thread2 %ld returns %d\n",pthread_self(),\
pthread_getspecific(key));
my_errno = 2;
print_errno("thread2");
}
void *thread1(void *arg)
{
pthread_t thid2;
printf("thread1 %ld is running\n",pthread_self());
pthread_setspecific(key, (void *)my_errno);
my_errno = 1;
pthread_create(&thid2, NULL, thread2, NULL);
sleep(2);
printf("thread1 %ld returns %d\n",pthread_self(),\
pthread_getspecific(key));
print_errno("thread1");
}
void destr(void *arg)
{
printf("destroy memory\n");
}
int main(void)
{
pthread_t thid1;
printf("main thread begins running. my_errno=%d\n",my_errno);
pthread_key_create(&key, destr);
pthread_create(&thid1, NULL, thread1, NULL);
sleep(4);
pthread_key_delete(key);
printf("main thread exit\n");
return 0;
}