Linux中pthread_detach()线程注意

本文介绍了两种创建分离线程的方法,并通过实例演示了如何在C语言环境下设置线程为分离状态,探讨了分离线程与主线程的关系及资源回收机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、创建分离线程

有两种方式创建分离线程:

(1)在线程创建时将其属性设为分离状态(detached);

(2)在线程创建后将其属性设为分离的(detached)。

二、分离线程的作用

由系统来回收线程所占用资源。

三、实例

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <time.h>

void* thread1(void *arg)
{
    while (1)
    {
        usleep(100 * 1000);
        printf("thread1 running...!\n");
    }
    printf("Leave thread1!\n");

    return NULL;
}

int main(int argc, char** argv)
{
    pthread_t tid;

    pthread_create(&tid, NULL, (void*)thread1, NULL);
    pthread_detach(tid);  // 使线程处于分离状态
    sleep(1);
    printf("Leave main thread!\n");
    pthread_exit("end"); //这个地方执行后,子进程并没有退出
//    return 0; //return后,系统会调用_exit,所有进程都会退出。
}
 

#gcc a.c -lpthread

#./a.out
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
Leave main thread!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!

可以看出main线程退出后,thread_1并没有退出。

$ cat a.c
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <time.h>

void* thread1(void *arg)
{
    while (1)
    {
        usleep(100 * 1000);
        printf("thread1 running...!\n");
    }
    printf("Leave thread1!\n");

    return NULL;
}

int main(int argc, char** argv)
{
    pthread_t tid;

    pthread_create(&tid, NULL, (void*)thread1, NULL);
    pthread_detach(tid);  // 使线程处于分离状态
    sleep(1);
    printf("Leave main thread!\n");
    return 0;
}

$ ./a.out
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
Leave main thread!

可以看出main线程return后,子线程也退出了。

如果把pthread_deteach改为pthread_join,因为子线程是while(1)所有主线程也不会退出。

linjuntao@linjuntao:~/work/mt8516-p1v2/build/tmp/deploy/images/aud8516p1v2-slc-bluez$ cat a.c
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <time.h>

void* thread1(void *arg)
{
    while (1)
    {
        usleep(100 * 1000);
        printf("thread1 running...!\n");
    }
    printf("Leave thread1!\n");

    return NULL;
}

int main(int argc, char** argv)
{
    pthread_t tid;

    pthread_create(&tid, NULL, (void*)thread1, NULL);
    //pthread_detach(tid);  // 使线程处于分离状态
    pthread_join(tid,NULL);  // 使线程处于分离状态
    sleep(1);
    printf("Leave main thread!\n");
    return 0;
}

linjuntao@linjuntao:~/work/mt8516-p1v2/build/tmp/deploy/images/aud8516p1v2-slc-bluez$ ./a.out
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!
thread1 running...!

^

这可以在main中使用pthread_cancel,明确退出子线程。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值