【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;
}

测试结果

相关推荐

  1. Linux系统编程_文件编程

    2024-05-01 14:48:05       13 阅读
  2. Linux 系统编程

    2024-05-01 14:48:05       19 阅读
  3. Linux系统编程】缓冲区

    2024-05-01 14:48:05       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-01 14:48:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-01 14:48:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-01 14:48:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-01 14:48:05       20 阅读

热门阅读

  1. FastStone Capture:屏幕捕获与编辑的全能助手

    2024-05-01 14:48:05       9 阅读
  2. useLayoutEffect 和useEffect区别

    2024-05-01 14:48:05       8 阅读
  3. 保障互联网基础:深度解析DNS安全

    2024-05-01 14:48:05       10 阅读
  4. 存在矛盾的题目

    2024-05-01 14:48:05       8 阅读
  5. 【Docker学习】docker run的--annotation选项

    2024-05-01 14:48:05       12 阅读
  6. 泰勒创造力达到顶峰?(下)

    2024-05-01 14:48:05       9 阅读
  7. 美国国防部数据网格参考架构概述(上)

    2024-05-01 14:48:05       9 阅读
  8. Redis 常见的使用场景

    2024-05-01 14:48:05       8 阅读
  9. PCL 在点云中提取其子集点云(点云索引提取)

    2024-05-01 14:48:05       7 阅读
  10. 实验8-13:补全代码,删除字符数组中多个字符

    2024-05-01 14:48:05       9 阅读