pthead 创建与销毁详解 pthread_create pthread_join pthread_exit pthread_detach

2023-10-27 15:44

本文主要是介绍pthead 创建与销毁详解 pthread_create pthread_join pthread_exit pthread_detach,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pthead 创建与销毁

pthread_create

  • 函数原型:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine)(void *), void *arg);
    
    • thread:指向 pthread_t 类型的指针,用于存储新线程的标识符。
    • attr:指向 pthread_attr_t 类型的指针,用于设置新线程的属性,为 NULL 则使用默认属性。
    • start_routine:指向函数的指针,新线程的运行入口。
    • arg:传递给 start_routine 的参数,可以为 NULL。
    • 返回值:成功返回 0,失败返回错误代码。
  • 创建一个新的线程,并以 start_routine 指定的函数作为入口而开始执行。

  • 新线程可以通过以下方式停止:

    • 自身调用 pthread_exit,其结束状态码可以在主进程中通过 pthread_join 获得;
    • start_routine 返回,返回值等同于 pthread_exit 的结束状态码;
    • 在主线程中调用 pthread_cancel
    • 主进程中的任何线程调用 exit 函数或者主进程从 main 返回。
  • 新线程的 cpu time clock0

  • 新线程将继承其父进程的 cpu 亲和性。

pthread_exit

  • 函数原型:

    void pthread_exit(void *retval);
    
    • retval:线程的退出状态,可以是任意类型的指针,可以为 NULL。
  • 立即终止当前线程的执行,并将 retval 作为线程的退出状态返回给调用者。线程的退出状态可以通过其他线程使用 pthread_join 函数来获取。

pthread_join

  • 函数原型:

    int pthread_join(pthread_t thread, void **retval);
    
    • thread:要等待的线程标识符,其必须为 join-able。
    • retval:指向指针的指针,用于存储被等待线程的返回值,可以为 NULL。
    • 返回值:成功返回 0,失败返回错误代码。
  • 阻塞调用它的线程,等待指定线程的结束,并获取其返回值。如果指定的线程早已结束,则立即返回。

  • 以下代码演示了 pthread_create,pthread_join,pthread_exit 的使用:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>#define NEED_EXIT_STATUS (0)void *thread_func(void *arg)
    {printf("thread started\n");int *value = (int *)arg;// 输出为 10printf("received value: %d\n", *value);// 修改传入的值*value = 100;sleep(1);#if NEED_EXIT_STATUS// 需要返回结束状态码, 以下两种方法都可以,推荐使用 return// pthread_exit((void *)value);return value;
    #else// 不需要返回结束状态码,以下两种方法都可以,推荐使用 return// pthread_exit(NULL);return NULL;
    #endif
    }int main()
    {pthread_t tid;int value = 10;// 创建一个新线程,并以 thread_func 作为入口函数,传递给 thread_func 函数的参数为 valueint ret = pthread_create(&tid, NULL, thread_func, (void *)&value);if (ret != 0) {fprintf(stderr, "pthread_create error: %d\n", ret);return -1;}printf("created new thread with id %ld\n", tid);#if NEED_EXIT_STATUS// 等待线程结束并获取退出状态void *thread_retval;ret = pthread_join(tid, &thread_retval);if (ret != 0) {fprintf(stderr, "pthread_join error: %d\n", ret);return -1;}int *retval = (int *)thread_retval;printf("thread exited with value: %d\n", *retval);
    #else// 等待线程结束不获取退出状态ret = pthread_join(tid, NULL);if (ret != 0) {fprintf(stderr, "pthread_join error: %d\n", ret);return -1;}printf("thread %ld exit normal\n", tid);
    #endifreturn 0;
    }
    

pthread_detach

  • 函数原型:

    int pthread_detach(pthread_t thread);
    
    • thread:要设置为分离状态的线程标识符。
    • 返回值:成功返回 0,失败返回错误代码。
  • 将指定线程设置为“分离状态”,使得该线程结束时能够自动释放其资源。

  • 一旦线程被设置为分离状态,就不能再调用 pthread_join 等待它的结束。

  • 以下代码演示了 pthread_detach 的使用:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>#define NEED_EXIT_STATUS (0)void *thread_func(void *arg)
    {printf("thread started\n");sleep(1);printf("thread exited\n");return NULL;
    }int main() {pthread_t tid;int ret = pthread_create(&tid, NULL, thread_func, NULL);if (ret != 0) {fprintf(stderr, "pthread_create error: %d\n", ret);return -1;}printf("created new thread with id %ld\n", tid);ret = pthread_detach(tid);if (ret != 0) {fprintf(stderr, "pthread_detach error: %d\n", ret);return -1;}printf("thread detached, main thread exited\n");return 0;
    }
    

这篇关于pthead 创建与销毁详解 pthread_create pthread_join pthread_exit pthread_detach的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/286661

相关文章

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

Node.js 数据库 CRUD 项目示例详解(完美解决方案)

《Node.js数据库CRUD项目示例详解(完美解决方案)》:本文主要介绍Node.js数据库CRUD项目示例详解(完美解决方案),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考... 目录项目结构1. 初始化项目2. 配置数据库连接 (config/db.js)3. 创建模型 (models/

SQL表间关联查询实例详解

《SQL表间关联查询实例详解》本文主要讲解SQL语句中常用的表间关联查询方式,包括:左连接(leftjoin)、右连接(rightjoin)、全连接(fulljoin)、内连接(innerjoin)、... 目录简介样例准备左外连接右外连接全外连接内连接交叉连接自然连接简介本文主要讲解SQL语句中常用的表

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

Python中局部变量和全局变量举例详解

《Python中局部变量和全局变量举例详解》:本文主要介绍如何通过一个简单的Python代码示例来解释命名空间和作用域的概念,它详细说明了内置名称、全局名称、局部名称以及它们之间的查找顺序,文中通... 目录引入例子拆解源码运行结果如下图代码解析 python3命名空间和作用域命名空间命名空间查找顺序命名空

SpringRetry重试机制之@Retryable注解与重试策略详解

《SpringRetry重试机制之@Retryable注解与重试策略详解》本文将详细介绍SpringRetry的重试机制,特别是@Retryable注解的使用及各种重试策略的配置,帮助开发者构建更加健... 目录引言一、SpringRetry基础知识二、启用SpringRetry三、@Retryable注解

springboot项目中常用的工具类和api详解

《springboot项目中常用的工具类和api详解》在SpringBoot项目中,开发者通常会依赖一些工具类和API来简化开发、提高效率,以下是一些常用的工具类及其典型应用场景,涵盖Spring原生... 目录1. Spring Framework 自带工具类(1) StringUtils(2) Coll

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2

在PyCharm中安装PyTorch、torchvision和OpenCV详解

《在PyCharm中安装PyTorch、torchvision和OpenCV详解》:本文主要介绍在PyCharm中安装PyTorch、torchvision和OpenCV方式,具有很好的参考价值,... 目录PyCharm安装PyTorch、torchvision和OpenCV安装python安装PyTor