【Linux系统编程】30.pthread_exit、pthread_join、pthread_cancel

目录

pthread_exit

参数retval

测试代码1

测试结果

pthread_join

参数thread

参数retvsl

返回值

测试代码2

测试结果

pthread_cancel

参数thread

返回值

测试代码3

测试结果

pthread_exit

退出当前线程。

man 3 pthread_exit

参数retval

退出值。

NULL:无退出值。

测试代码1

退出某一个线程。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>

void *HuiDiao_HanShu(void *arg)
{
    int num;
    num = (int)arg + 1;
    sleep(num);
    if (num == 3) //退出第3个子线程
    {
        pthread_exit(NULL);
    }
    printf("这是第%d个子线程的回调函数,子线程的进程ID是%d,子线程ID是%lu。\n", num, getpid(), pthread_self());
}

int main(int argc, char *argv[])
{
    int flag, i;
    pthread_t ZiXianCheng_ID; //子线程ID
    for (i = 0; i < 5; i++)
    {
        flag = pthread_create(&ZiXianCheng_ID, NULL, HuiDiao_HanShu, (void *)i); //创建子线程
        if (flag != 0)
        {
            perror("创建子线程错误");
            exit(1);
        }
    }

    printf("这是主线程,进程ID是%d,线程ID是%lu。\n", getpid(), pthread_self());
    sleep(i + 1);

    return 0;
}

测试结果

pthread_join

阻塞等待线程退出,获取线程退出状态。

man 3 pthread_join

参数thread

线程ID。

参数retvsl

存储线程结束状态。被杀死的线程:-1

返回值

成功:0

失败:错误号

测试代码2

阻塞等待线程退出。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>

struct CeShi
{
    int a;
    char str[256];
};

void *HuiDiao_HanShu(void *arg)
{
    struct CeShi *temp;
    temp = malloc(sizeof(temp));
    temp->a = 100;
    strcpy(temp->str, "你好,世界!\n");
    printf("这是子线程的回调函数,子线程的进程ID是%d,子线程ID是%lu。\n", getpid(), pthread_self());
    return (void *)temp;
}

int main(int argc, char *argv[])
{
    int flag;
    pthread_t ZiXianCheng_ID; //子线程ID
    struct CeShi *temp1;
    flag = pthread_create(&ZiXianCheng_ID, NULL, HuiDiao_HanShu, NULL); //创建子线程
    if (flag != 0)
    {
        perror("创建子线程错误");
        exit(1);
    }

    printf("这是主线程,进程ID是%d,线程ID是%lu。\n", getpid(), pthread_self());
    printf("开始回收子线程!\n");
    flag = pthread_join(ZiXianCheng_ID, (void **)&temp1);
    if (flag != 0)
    {
        perror("回收子线程错误");
        exit(1);
    }
    printf("回收子线程完成!\n");
    printf("a=%d,str=%s", temp1->a, temp1->str);
    pthread_exit(NULL);

    return 0;
}

测试结果

pthread_cancel

杀死一个线程,需要取消点或者系统调用才能完全杀死

man 3 pthread_cancel

参数thread

待杀死线程的ID。

返回值

成功:0,确认执行线程的死刑,具体执行时间未知

失败:错误号

测试代码3

杀死一个线程。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>

void *HuiDiao_HanShu(void *arg)
{
    printf("这是子线程的回调函数,子线程的进程ID是%d,子线程ID是%lu。\n", getpid(), pthread_self());
    while (1)
    {
        //printf("这是子线程的回调函数,子线程的进程ID是%d,子线程ID是%lu。\n", getpid(), pthread_self());
        //sleep(1);
        pthread_testcancel(); //设置取消点
    }
    return (void *)0;
}

int main(int argc, char *argv[])
{
    int flag;
    pthread_t ZiXianCheng_ID; //子线程ID
    void *num = NULL;

    flag = pthread_create(&ZiXianCheng_ID, NULL, HuiDiao_HanShu, NULL); //创建子线程
    if (flag != 0)
    {
        perror("创建子线程错误");
        exit(1);
    }

    printf("这是主线程,进程ID是%d,线程ID是%lu。\n", getpid(), pthread_self());

    printf("这是主线程,先睡一会,再杀子线程!\n");
    sleep(3);
    printf("这是主线程,睡醒了!\n");

    printf("开始杀子线程!\n");
    flag = pthread_cancel(ZiXianCheng_ID);
    if (flag != 0)
    {
        perror("杀子线程错误");
        exit(1);
    }
    printf("子线程已杀死!\n");

    printf("开始回收子线程!\n");
    flag = pthread_join(ZiXianCheng_ID, &num);
    if (flag != 0)
    {
        perror("回收子线程错误");
        exit(1);
    }
    printf("回收子线程完成!\n");
    printf("num=%d\n", (int)num);

    pthread_exit(NULL);

    return 0;
}

测试结果

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因心,三人水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值