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

相关文章

详解MySQL中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程