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

相关文章

javaSE类和对象进阶用法举例详解

《javaSE类和对象进阶用法举例详解》JavaSE的面向对象编程是软件开发中的基石,它通过类和对象的概念,实现了代码的模块化、可复用性和灵活性,:本文主要介绍javaSE类和对象进阶用法的相关资... 目录前言一、封装1.访问限定符2.包2.1包的概念2.2导入包2.3自定义包2.4常见的包二、stati

SpringBoot结合Knife4j进行API分组授权管理配置详解

《SpringBoot结合Knife4j进行API分组授权管理配置详解》在现代的微服务架构中,API文档和授权管理是不可或缺的一部分,本文将介绍如何在SpringBoot应用中集成Knife4j,并进... 目录环境准备配置 Swagger配置 Swagger OpenAPI自定义 Swagger UI 底

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

Linux权限管理与ACL访问控制详解

《Linux权限管理与ACL访问控制详解》Linux权限管理涵盖基本rwx权限(通过chmod设置)、特殊权限(SUID/SGID/StickyBit)及ACL精细授权,由umask决定默认权限,需合... 目录一、基本权限概述1. 基本权限与数字对应关系二、权限管理命令(chmod)1. 字符模式语法2.

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

Linux线程同步/互斥过程详解

《Linux线程同步/互斥过程详解》文章讲解多线程并发访问导致竞态条件,需通过互斥锁、原子操作和条件变量实现线程安全与同步,分析死锁条件及避免方法,并介绍RAII封装技术提升资源管理效率... 目录01. 资源共享问题1.1 多线程并发访问1.2 临界区与临界资源1.3 锁的引入02. 多线程案例2.1 为

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的