Linux学习之多线程编程(线程私有属性)

言之者无罪,闻之者足以戒。 ——《诗序》

4、线程私有属性

应用程序设计中有必要提供一种变量,使得多个函数多个线程都可以访问这个变量(看起来是个全局变量),但是线程对这个变量的访问都不会彼此产生影响(貌似不是全局变量哦),但是你需要这样的数据,比如errno。那么这种数据就是线程的私有数据,尽管名字相同,但是每个线程访问的都是数据的副本。

在使用私有数据之前,你首先要创建一个与私有数据相关的键,要来获取对私有数据的访问权限 。这个键的类型是pthread_key_t

(1)、pthread_key_create创建私有数据的键

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))

第一个参数:创建一个键值

第二个参数:析构函数

返回值:成功返回0,失败返回错误码

创建的键放在key指向的内存单元,destructor是与键相关的析构函数。当线程调用pthread_exit或者使用return返回,析构函数就会被调用。当析构函数调用的时候,它只有一个参数,这个参数是与key关联的那个数据的地址(也就是你的私有数据啦),因此你可以在析构函数中将这个数据销毁。

键使用完之后也可以销毁,当键销毁之后,与它关联的数据并没有销毁哦

扫描二维码关注公众号,回复: 3708543 查看本文章

(2)、pthread_key_delete销毁私有数据的键

int pthread_key_delete(pthread_key_t key)

参数:要销毁的键

返回值:成功返回0,失败返回错误码

有了键之后,你就可以将私有数据和键关联起来,这样就就可以通过键来找到数据。所有的线程都可以访问这个键,但他们可以为键关联不同的数据。(这岂不是一个名字一样,而值却不同的全局变量么)

(3)、pthread_setspecific私有数据与键值关联函数

int pthread_setspecific(pthread_key_t key, const void *value)

第一个参数:要关联的键

第二个参数:关联的数据

返回值 :成功返回0,失败返回错误码

(4)、*pthread_getspecific获取私有数据的地址

void *pthread_getspecific(pthread_key_t key)

参数:关联的键值

返回值:有数据关联返回数据值,没有数据关联返回NULL

下面看一下程序代码:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>


pthread_key_t key;

void *thread_fun1(void *arg)
{
        printf("xiaoyi one\n");
        int a=6;
        //将key与a关联
        pthread_setspecific(key,(void *)a);
        sleep(2);
        printf("xiaoyi one key data is %d\n",pthread_getspecific(key));
}
void *thread_fun2(void *arg)
{
        sleep(1);
        printf("xiaoyi two\n");
        int a=10;
        //将key与a关联
        pthread_setspecific(key,(void *)a);
        printf("xiaoyi two key data is %d\n",pthread_getspecific(key));
}

int main()
{
        pthread_t tid1,tid2;
        //创造一个key
        pthread_key_create(&key,NULL);

        //创造新的线程
        if(pthread_create(&tid1,NULL,thread_fun1,NULL))
        {
                printf("create new thread failure\n");
                return ;
        }

        if(pthread_create(&tid2,NULL,thread_fun2,NULL))
        {
                printf("create new thread failure\n");
                return ;
        }
        //等待线程的结束
        pthread_join(tid1,NULL);
        pthread_join(tid2,NULL);

        return 0;
}

5、线程与fork

命令:ps 查看进程

命令:kii 进程inode号   (杀死一个死线程)

当线程调用fork函数时,就为子进程创建了整个进程地址空间的副本,子进程通过继承整个地址空间的副本,也会将父进程的互斥量、读写锁、条件变量的状态继承过来。也就是说,如果父进程中互斥量是锁着的,那么在子进程中互斥量也是锁着的(尽管子进程自己还没有来得及lock),这是非常不安全的,因为不是子进程自己锁住的,它无法解锁。

子进程内部只有一个线程,由父进程中调用fork函数的线程副本构成。如果调用fork的线程将互斥量锁住,那么子进程会拷贝一个pthread_mutex_lock副本,这样子进程就有机会去解锁了。或者互斥量根本就没被加锁,这样也是可以的,但是你不能确保永远是这样的情况。

pthread_atfork函数给你创造了这样的条件,它会注册三个函数

(1)、pthread_atfork

int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))

第一个参数:获取锁状态的函数

第二个参数:fork创建的父进程

第三个参数:fork创建的子进程

返回值:成功 返回0,失败返回错误码

prepare是在fork调用之前会被调用的,parent在fork返回父进程之前调用,child在fork返回子进程之前调用。如果在prepare中加锁所有的互斥量,在parent和child中解锁所有的互斥量,那么在fork返回之后,互斥量的状态就是未加锁。

可以有多个 pthread_atfork函数,这时也就会有多个处理程序(prepare,parent,child)。多个prepare的执行顺序与注册顺序相反,而parent和child的执行顺序与注册顺序相同。

下面来看一下程序代码:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void prepare()
{
        pthread_mutex_lock(&mutex);
        printf("I am prepare\n");
}
void parent()
{
        pthread_mutex_unlock(&mutex);
        printf("I am parent\n");
}
void child()
{
        pthread_mutex_unlock(&mutex);
        printf("I am child\n");
}
void *thread_fun(void *arg)
{
        sleep(1);
        pid_t pid;
        pthread_atfork(prepare,parent,child);
        pid=fork();
        if(pid == 0)
        {
                pthread_mutex_lock(&mutex);
                printf("child process\n");
                pthread_mutex_unlock(&mutex);
        }
        if(pid > 0)
        {
                pthread_mutex_lock(&mutex);
                printf("parent process\n");
                pthread_mutex_unlock(&mutex);
        }
}

int main()
{
        pthread_t tid;
        if(pthread_create(&tid,NULL,thread_fun,NULL))
        {
                printf("create new thread failure\n");
                return ;
        }

        pthread_mutex_lock(&mutex);
        sleep(1);
        printf("main process\n");
        pthread_mutex_unlock(&mutex);
        pthread_join(tid,NULL);

        pthread_mutex_destroy(&mutex);
        return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42994525/article/details/83244902