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

相关文章

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

详解python pycharm与cmd中制表符不一样

《详解pythonpycharm与cmd中制表符不一样》本文主要介绍了pythonpycharm与cmd中制表符不一样,这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽... 这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽度不同导致的。在PyChar