【Linux】线程控制-线程终止

如果需要只终⽌某个线程⽽不终⽌整个进程,可以有三种⽅法:

1. 从线程函数return。这种⽅法对主线程不适⽤,从main函数return相当于调⽤exit。

#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void* thr_start(void* arg)
{
    int ret = (int)arg;
    while(1)
    {
        printf("child pthread!! %d \n", ret);
        sleep(1);

    }

    return NULL;
}

int main()
{
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thr_start, (void*)88);
    if(0 != ret)
    {
        printf("create pthread error\n");
        return -1;
    }

    while(1)
    {
        printf("main pthread\n");
        sleep(1);
    }

    return 0;
}

从上面很容易看出我创建了永远不会退出的线程,并且主线程也永远不会退出。事实上也是如此,看运行结果:

现在我们用return退出自己创建的线程,用exit(0)退出主线程,也就是退出了进程。

#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void* thr_start(void* arg)
{
    int ret = (int)arg;
    while(1)
    {
        printf("child pthread!! %d \n", ret);
        sleep(4);
        return NULL;

    }

    return NULL;
}

int main()
{
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thr_start, (void*)88);
    if(0 != ret)
    {
        printf("create pthread error\n");
        return -1;
    }

    while(1)
    {
        printf("main pthread\n");
        sleep(5);
        exit(0);
    }

    return 0;
}

可以看出,两个线程都只运行了一遍while循环就直接退出了。

2.线程可以调用pthread_exit终止自己

功能:线程终⽌
原型
 void pthread_exit(void *value_ptr);
参数
 value_ptr:value_ptr不要指向⼀个局部变量。
返回值:⽆返回值,跟进程⼀样,线程结束的时候⽆法返回到它的调⽤者(⾃⾝)
#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void* thr_start(void* arg)
{
    int ret = (int)arg;
    while(1)
    {
        printf("child pthread!! %d \n", ret);
        sleep(5);
       pthread_exit(NULL);


    }

    return NULL;
}

int main()
{
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thr_start, (void*)88);
    if(0 != ret)
    {
        printf("create pthread error\n");
        return -1;
    }

    sleep(3);
    pthread_exit(NULL);

    while(1)
    {
        printf("main pthread\n");
        sleep(5);

    }

    return 0;
}

我给两个线程都调用了pthread_exit函数,只不过我让主线程先于子线程退出,这样就成了僵尸线程,不过和僵尸进程不同的是,main()函数结束,进程所有资源都会被回收,包括僵尸线程。而且一般情况只有主线程才会显示僵尸线程。

如果只结束主线程,把子线程的pthread_exit注释掉,那么主线程依旧是僵尸线程Z,子线程还是可中断睡眠状态S。

3. ⼀个线程可以调⽤pthread_ cancel终⽌同⼀进程中的另⼀个线程。

功能:取消⼀个执⾏中的线程
原型
 int pthread_cancel(pthread_t thread);
参数
 thread:线程ID
返回值:成功返回0;失败返回错误码

我在主线程中调用pthread_cancel来终止子线程。

#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void* thr_start(void* arg)
{
    int ret = (int)arg;
    while(1)
    {
        printf("child pthread!! %d \n", ret);
        sleep(1);

    }

    return NULL;
}

int main()
{
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thr_start, (void*)88);
    if(0 != ret)
    {
        printf("create pthread error\n");
        return -1;
    }

    sleep(3);
    pthread_cancel(tid);
    while(1)
    {
        printf("main pthread\n");
        sleep(1);
    }

    return 0;
}

很明显子线程运行了3秒后被终止了。

如果想用pthread_cancel来终止自己改怎么办?

其实也简单,把自己的 线程ID传入即可。

来看一个函数:

pthread_t pthread_self(void);
返回调用线程的tid

测试一下子线程结束掉自己。

#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void* thr_start(void* arg)
{

    pthread_t tid = pthread_self();
    int ret = (int)arg;
    while(1)
    {
        printf("child pthread!! %d \n", ret);
        sleep(1);
        pthread_cancel(tid);
    }

    return NULL;
}

int main()
{
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thr_start, (void*)88);
    if(0 != ret)
    {
        printf("create pthread error\n");
        return -1;
    }
    
    while(1)
    {
        printf("main pthread\n");
        sleep(1);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值