当前位置: 首页 > news >正文

第三章 LInux多线程开发 3.1-3.5线程创建 终止 分离

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

创建线程:(好好记住 可能会叫写代码)

一般情况下,main函数所在的线程我们称之为主线程(main线程),其余创建的线程称之为子线程。 程序中默认只有一个进程,fork()函数调用,2进行
程序中默认只有一个线程,pthread_create()函数调用,2个线程。

#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);

    - 功能:创建一个子线程- 参数:- thread:传出参数,线程创建成功后,子线程的线程ID被写到该变量中。- attr : 设置线程的属性,一般使用默认值,NULL- start_routine : 函数指针,这个函数是子线程需要处理的逻辑代码- arg : 给第三个参数使用,传参- 返回值:成功:0失败:返回错误号。这个错误号和之前errno不太一样。获取错误号的信息:  char * strerror(int errnum);
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>void * callback(void * arg) {printf("child thread...\n");printf("arg value: %d\n", *(int *)arg);return NULL;
}int main() {pthread_t tid;int num = 10;// 创建一个子线程int ret = pthread_create(&tid, NULL, callback, (void *)&num);if(ret != 0) {char * errstr = strerror(ret);printf("error : %s\n", errstr);} for(int i = 0; i < 5; i++) {printf("%d\n", i);}sleep(1);return 0;   // exit(0);
}

终止线程

/*#include <pthread.h>void pthread_exit(void *retval);功能:终止一个线程,在哪个线程中调用,就表示终止哪个线程参数:retval:需要传递一个指针,作为一个返回值,可以在pthread_join()中获取到。pthread_t pthread_self(void);功能:获取当前的线程的线程IDint pthread_equal(pthread_t t1, pthread_t t2);功能:比较两个线程ID是否相等不同的操作系统,pthread_t类型的实现不一样,有的是无符号的长整型,有的是使用结构体去实现的。
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>void * callback(void * arg) {printf("child thread id : %ld\n", pthread_self());return NULL;    // pthread_exit(NULL);
} int main() {// 创建一个子线程pthread_t tid;int ret = pthread_create(&tid, NULL, callback, NULL);if(ret != 0) {char * errstr = strerror(ret);printf("error : %s\n", errstr);}// 主线程for(int i = 0; i < 5; i++) {printf("%d\n", i);}printf("tid : %ld, main thread id : %ld\n", tid ,pthread_self());// 让主线程退出,当主线程退出时,不会影响其他正常运行的线程。pthread_exit(NULL);printf("main thread exit\n");return 0;   // exit(0);
}

连接已终止的线程

/*#include <pthread.h>int pthread_join(pthread_t thread, void **retval);- 功能:和一个已经终止的线程进行连接回收子线程的资源这个函数是阻塞函数,调用一次只能回收一个子线程一般在主线程中使用- 参数:- thread:需要回收的子线程的ID- retval: 接收子线程退出时的返回值- 返回值:0 : 成功非0 : 失败,返回的错误号
*/#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>int value = 10;void * callback(void * arg) {printf("child thread id : %ld\n", pthread_self());// sleep(3);// return NULL; // int value = 10; // 局部变量pthread_exit((void *)&value);   // return (void *)&value;
} int main() {// 创建一个子线程pthread_t tid;int ret = pthread_create(&tid, NULL, callback, NULL);if(ret != 0) {char * errstr = strerror(ret);printf("error : %s\n", errstr);}// 主线程for(int i = 0; i < 5; i++) {printf("%d\n", i);}printf("tid : %ld, main thread id : %ld\n", tid ,pthread_self());// 主线程调用pthread_join()回收子线程的资源int * thread_retval;ret = pthread_join(tid, (void **)&thread_retval);if(ret != 0) {char * errstr = strerror(ret);printf("error : %s\n", errstr);}printf("exit data : %d\n", *thread_retval);printf("回收子线程资源成功!\n");// 让主线程退出,当主线程退出时,不会影响其他正常运行的线程。pthread_exit(NULL);return 0; 
}

线程的分离

/*#include <pthread.h>int pthread_detach(pthread_t thread);- 功能:分离一个线程。被分离的线程在终止的时候,会自动释放资源返回给系统。1.不能多次分离,会产生不可预料的行为。2.不能去连接一个已经分离的线程,会报错。- 参数:需要分离的线程的ID- 返回值:成功:0失败:返回错误号
*/
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>void * callback(void * arg) {printf("chid thread id : %ld\n", pthread_self());return NULL;
}int main() {// 创建一个子线程pthread_t tid;int ret = pthread_create(&tid, NULL, callback, NULL);if(ret != 0) {char * errstr = strerror(ret);printf("error1 : %s\n", errstr);}// 输出主线程和子线程的idprintf("tid : %ld, main thread id : %ld\n", tid, pthread_self());// 设置子线程分离,子线程分离后,子线程结束时对应的资源就不需要主线程释放ret = pthread_detach(tid);if(ret != 0) {char * errstr = strerror(ret);printf("error2 : %s\n", errstr);}// 设置分离后,对分离的子线程进行连接 pthread_join()// ret = pthread_join(tid, NULL);// if(ret != 0) {//     char * errstr = strerror(ret);//     printf("error3 : %s\n", errstr);// }pthread_exit(NULL);return 0;
}
http://www.lryc.cn/news/156635.html

相关文章:

  • 空间曲线的参数方程
  • 非华为机型如何体验HarmonyOS鸿蒙系统 刷写HarmonyOS鸿蒙GSI系统以及一些初步的bug修复
  • Flutter 生成小程序的混合 App 实践
  • 利用 Python-user-agents 解析 User_Agent
  • Java版企业电子招标采购系统源码Spring Cloud + Spring Boot +二次开发+ MybatisPlus + Redis
  • Mybatis如何给字段起别名?
  • php对接AWS S3云存储,上传S3及访问权限问题
  • java 实现单例模式
  • minio文件服务器开启https
  • 每日刷题(回溯法经典问题之子集)
  • PostgreSQL在进行除法时要注意
  • 开开心心带你学习MySQL数据库之第五篇
  • Geotools对geojson的解析
  • 【博客701】shell实现保留网络现场:ping失败时执行mtr
  • 放弃手写代码吧!用低代码你能生成各种源码
  • 什么程度才算精通 Linux?
  • jmeter中的__setProperty用法
  • vue基础知识六:v-show和v-if有什么区别?使用场景分别是什么?
  • SpringBoot几个常用的注解
  • 腾讯JAVA后端秋招面试总结
  • 随着iPhone 15降临,是时候扔掉所有的Lightning充电器了
  • huggingface 使用入门笔记
  • ASP.NET Core 中的 Razor Pages
  • C语言入门 Day_14 for循环
  • 深入解析 Socks5 代理与网络安全
  • Vue + Element UI 前端篇(十二):用户管理模块
  • C# 设计保存文件
  • Leetcode 1486.数组异或操作
  • 【Java】Java核心API概述
  • 微信小程序检查版本更新