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

Linux下的线程操作

一、多线程的创建于退出

1. pthread_create(线程的创建)

pthread_create 是 POSIX 线程库中的函数,用于创建一个新的线程。
函数原型如下:

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:传递给线程函数的参数,可以是任意类型的指针。

pthread_create 函数会创建一个新的线程,并在新线程中执行指定的线程函数 start_routine。线程函数的参数可以通过 arg 传递。

2.ptherad_join(等待指定线程的终止)

pthread_join 是 POSIX 线程库中的函数,用于等待指定的线程终止,并获取线程的退出状态。
函数原型如下:

int pthread_join(pthread_t thread, void **retval);

参数说明:

  • thread:要等待的线程的标识符。
  • retval:指向指针的指针,用于存储线程的退出状态。如果不需要获取退出状态,可以传递 NULL。

pthread_join 函数会阻塞当前线程,直到指定的线程终止。一旦线程终止,pthread_join 函数会返回,并将线程的退出状态存储在 retval 指向的位置。如果不需要获取退出状态,可以将 retval 设置为 NULL。

3.pthread_exit(线程的退出)

pthread_exit 是 POSIX 线程库中的函数,用于终止当前线程并返回一个退出状态。
函数原型如下:

void pthread_exit(void *retval);

参数说明:

  • retval:线程的退出状态,可以是任意类型的指针。

pthread_exit 函数会立即终止当前线程,并将 retval 参数作为线程的退出状态。线程的退出状态可以是任意类型的指针,因为 pthread_exit 函数的参数类型是 void*。

4.pthread_cancel(线程的取消)

pthread_cancel 是 POSIX 线程库中的函数,用于取消指定的线程。
函数原型如下:

int pthread_cancel(pthread_t thread);

参数说明:

  • thread:要取消的线程的标识符。

5.pthread_setcancelstate(设置线程的取消状态)

pthread_setcancelstate 是 POSIX 线程库中的函数,用于设置线程的取消状态。
函数原型如下:

int pthread_setcancelstate(int state, int *oldstate);

参数说明:

  • state:要设置的取消状态,可以是以下两个值之一:
    • PTHREAD_CANCEL_ENABLE:启用线程的取消功能。
    • PTHREAD_CANCEL_DISABLE:禁用线程的取消功能。
  • oldstate:用于存储之前的取消状态的指针

pthread_setcancelstate 函数用于设置线程的取消状态。取消状态决定了线程是否可以被取消。如果取消状态被设置为 PTHREAD_CANCEL_ENABLE,则线程可以被取消;如果取消状态被设置为 PTHREAD_CANCEL_DISABLE,则线程不会被取消。
当线程被取消时,会根据取消类型的设置来决定线程的行为。取消类型可以通过 pthread_setcanceltype 函数设置。

6.例子

#include <stdio.h>                                                                                                                                  
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>pthread_t tid[2];//void *类型的函数可以没有显示的返回值
void *my_thread1(void *arg)
{for(int i = 0; i < 3; i++){   printf("this is my_thread1\n");sleep(1);}//取消线程2pthread_cancel(tid[1]);//线程退出
//  pthread_exit((void *)100);return (void *)100;
}void *my_thread2(void *arg)
{//修改属性,不能被取消int old;pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);for(int i = 0; i < 5; i++){   printf("%s\n", (char *)arg);sleep(1);}   
}int main()
{ //1.线程号 2.线程属性 3.线程函数 3.线程函数参数if(pthread_create(&tid[0], NULL, my_thread1, NULL) != 0){perror("pthread_create");exit(1);}//虽然 "helloworld" 是一个字符串常量,但它在 C 语言中被视为字符数组的首地址,//因此可以将其传递给 pthread_create 函数作为参数。if(pthread_create(&tid[1], NULL, my_thread2, "helloworld") != 0){perror("ptherad_create");exit(2);}//主线程一定不能提前结束//主线程等待,直到两个线程都结束void *status;pthread_join(tid[0], &status); //等待子线程结束,回收线程资源printf("线程1结束: %d\n", (int)status);pthread_join(tid[1], &status);printf("线程2结束\n");return 0;
}                   
http://www.lryc.cn/news/293229.html

相关文章:

  • 机器学习 | 如何利用集成学习提高机器学习的性能?
  • [Python] 什么是PCA降维技术以及scikit-learn中PCA类使用案例(图文教程,含详细代码)
  • npm 淘宝镜像正式到期,更新使用成功
  • python_蓝桥杯刷题记录_笔记_全AC代码_入门2
  • 备战蓝桥杯---数据结构与STL应用(入门4)
  • 2023_12蓝桥杯STEMA 考试 Scratch 中级试卷解析
  • 从编程中理解:大脑中的杏仁核
  • Maven dependency中的scope
  • 代码随想录算法训练营DAY11 | 栈与队列 (2)
  • 【Spring实战】33 Spring Boot3 集成 Nacos 配置中心
  • ElementUI安装与使用指南
  • Opencv——图片卷积
  • 项目安全-----加密算法实现
  • 只用一台服务器部署上线(宝塔面板) 前后端+数据库
  • 《Pandas 简易速速上手小册》第8章:Pandas 高级数据分析技巧(2024 最新版)
  • 计算机网络_1.6.2 计算机网络体系结构分层的必要性
  • 跟着cherno手搓游戏引擎【18】抽象Shader、项目小修改
  • 每日OJ题_算法_模拟②_力扣495. 提莫攻击
  • freertos 源码分析二 list链表源码
  • Peter算法小课堂—Dijkstra最短路算法
  • Python 读取和写入包含中文的csv、xlsx、json文件
  • 【算法】利用递归dfs解决二叉树算法题(C++)
  • 计算机网络_1.6.1 常见的三种计算机网络体系结构
  • XML传参方式
  • Pyecharts炫酷散点图构建指南【第50篇—python:炫酷散点图】
  • 关于爬取所有哔哩哔哩、任意图片、所有音乐、的python脚本语言-Edge浏览器插件 全是干货!
  • 压力测试工具-Jmeter使用总结
  • [cmake]CMake Error: Could not create named generator Visual Studio 16 2019解决方法
  • 2024美赛数学建模D题思路分析 - 大湖区水资源问题
  • 2024 高级前端面试题之 HTTP模块 「精选篇」