[linux]进程(九)——线程

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值