linux下 c中怎么让才能安全关闭线程 和 linux线程退出时执行的程序(线程清理处理程序)简单例子

多线程退出有三种方式:
(1)执行完成后隐式退出;

(2)由线程本身显示调用pthread_exit 函数退出;
    pthread_exit (void * retval) ; 

(3)被其他线程用pthread_cance函数终止:
    pthread_cance (pthread_t thread) ; 


用event来实现。

在子线程中,在循环内检测event。
while(!e.is_active())
{
  ...
}
当退出循环体的时候,自然return返回。这样子线程会优雅的结束。

注意:选用非等待的检测函数。


pthread 线程有两种状态,joinable(非分离)状态和detachable(分离)状态,默认为joinable。

  joinable:当线程函数自己返回退出或pthread_exit时都不会释放线程所用资源,包括栈,线程描述符等(有人说有8k多,未经验证)。

  detachable:线程结束时会自动释放资源。

Linux man page said:

When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called  once  for each joinable thread created to avoid memory leaks.

因此,joinable 线程执行完后不使用pthread_join的话就会造成内存泄漏。

解决办法:

1.// 创建线程前设置 PTHREAD_CREATE_DETACHED 属性

pthread_attr_t attr;
pthread_t thread;
pthread_attr_init (&attr);
pthread_attr_setdetachstat(&attr, PTHREAD_CREATE_DETACHED);
pthread_create (&thread, &attr, &thread_function, NULL);
pthread_attr_destroy (&attr);


2.当线程为joinable时,使用pthread_join来获取线程返回值,并释放资源。

3.当线程为joinable时,也可在线程中调用 pthread_detach(pthread_self());来分离自己。


=========================================================================


/********************************** pthread_exit.c **************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>

void exe_exit1(void *arg)
{
    printf("我是线程退出时要执行的程序1!");
    printf("传递给我的参数是:%s\n",(char *)arg);
}

void exe_exit2(void *arg)
{
    printf("我是线程退出时要执行的程序2!");
    printf("传递给我的参数是:%s\n",(char *)arg);
}

void *func()
{
    int ret;
    
    ret=pthread_detach(pthread_self());/*使本线程成为分离状态的线程*/
    if(ret==0)
        printf("pthread_detach调用成功!\n");
    else
        printf("pthread_detach调用出错,错误编号:%d",ret,errno);
        
    printf("我是线程,已经开始启动!\n");
    pthread_cleanup_push(exe_exit2,"给exe_exit2的参数");
    pthread_cleanup_push(exe_exit1,"给exe_exit1的参数");
    printf("pthread_cleanup_push设置完成。\n");
    printf("我是线程,即将退出。\n");
    pthread_exit((void *)0);
    pthread_cleanup_pop(0);/*下面两行不能少,否则会编译不通过,提示语法错误*/
    pthread_cleanup_pop(0);
//    printf("pthread_cleanup_pop设置完成。\n");
}

int main()
{
    int ret;
    pthread_t tid;
    
    pthread_create(&tid,NULL,func,NULL);
    
    sleep(2);
    exit(0);
}

编译: gcc -o pthread_exit -lpthread pthread_exit.c
执行: ./pthread_exit

/ 运行结果 //
pthread_detach调用成功!
我是线程,已经开始启动!
pthread_cleanup_push设置完成。
我是线程,即将退出。
我是线程退出时要执行的程序1!传递给我的参数是:给exe_exit1的参数
我是线程退出时要执行的程序2!传递给我的参数是:给exe_exit2的参数

几个需要说明的地方:

1、pthread_cleanup_pop的参数是0的话,表示不执行线程清理处理程序(但并不妨碍把栈顶存放的那个处理程序的记录弹出,只是不执行而已),即这里的exe_exit1和exe_exit2。因此,如果程序中的这一句pthread_exit((void *)0);移到func函数结尾的话,exe_exit1和exe_exit2函数将不会得到执行,因为之前的两个pthread_cleanup_pop函数已经把存储的处理程序的记录给清空了。再执行到pthread_exit((void *)0);的时候栈中已经没有待执行的处理函数了。

2、虽然本程序中的pthread_cleanup_pop函数的参数是0,这两句本身不会触发线程清理处理程序(触发线程清理处理程序的任务是由pthread_exit来完成的)。但是这两句不能去掉,可以试一下把这两句注释掉,再编译的话就会出现错误。原因估计应该是pthread_cleanup_pop必须要和pthread_cleanup_push成对出现吧。

3、我一开始把pthread_detach函数放在了main函数中,结果运行时总是提示Segmentation fault(段错误)。后来在网上搜索后,改为把pthread_detach函数在func函数中调用,问题解决。 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值