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

linux高级学习10

24.9.7学习目录

  • 一.线程
    • 1.线程API

一.线程

线程与进程的关系:

  • 线程是轻量级进程,也有PCB,只是各自不同,创建线程使用的底层函数和进程一样,都是clone
  • 进程可以蜕变成线程
  • 线程是最小的执行单位,进程是最小的分配资源单位

1.线程API

(1)查看线程号

#include <pthread.h>
pthread_t pthread_self();

(2)创建线程

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

thread:线程标识符地址
attr:线程属性结构体地址,通常位NULL
start_routine:线程函数的入口地址
arg:传给线程函数的参数

(3)回收线程资源

等待线程结束,并回收线程资源

#include <pthread.h>
int pthread_join(pthread_t thread,void **retval);

thread:需要回收的线程号
retval:用来存储线程退出状态的指针的地址

(4)分离线程
将线程的回收工作分离出去,线程结束时,系统回收资源
其自身不带阻塞,所以需要有阻塞,不然到主函数结束了,线程还没来的及运行,就被迫结束

#include <pthread.h>
int pthread_detach(pthread_t thread);

(5)线程的取消和退出

#include <pthread.h>
//退出调用线程,当退出时所占用的资源并不会释放
void pthread_exit(void *retval);

retval:存储线程退出状态的指针

#include <pthread.h>
//杀死自己或者当前进程中的其他线程
int pthread_cancel(pthread_t thread);

线程的杀死不是实时的,是有一定的延迟,其需要达到取消点

(6)线程的属性
这些属性可以通过函数进行改变其数值,用于适应不同的情况

typedef struct
{int etachstate; //线程的分离状态int schedpolicy; //线程调度策略struct sched_param schedparam; //线程的调度参数int inheritsched; //线程的继承性int scope; //线程的作用域size_t guardsize; //线程栈末尾的警戒缓冲区大小int stackaddr_set; //线程的栈设置void* stackaddr; //线程栈的位置size_t stacksize; //线程栈的大小
}

http://www.mrgr.cn/news/21830.html

相关文章:

  • Hive和Hbase的区别
  • 最新SMS测压SMS源码 全新版本
  • 彻底解决win10系统Tomcat10控制台输出中文乱码
  • 漫谈设计模式 [15]:观察者模式
  • 架构全景视图
  • RocketMQ之发送消息源码分析
  • 漫谈设计模式 [5]:建造者模式
  • 脏页写入磁盘的过程详解
  • 理解C++全局对象析构顺序与 IPC 资源管理:避免 coredump
  • windows10 卸载网络驱动以及重新安装
  • 前端工程化1:从0到1配置eslint (所有人一看就懂)
  • 数据集 CrowdPose 多人姿态估计 深度学习 >> DataBall
  • sping boot 基于 RESTful 风格,模拟增删改查操作
  • ue5 伤害插件
  • springboot websocket 服务端
  • HarmonyOS】ArkTS学习之基于TextTimer的简易计时器的elapsedTime最小时间单位问题
  • Java项目: 基于SpringBoot+mybatis+maven网上租贸系统(含源码+数据库+毕业论文)
  • Computer Exercise
  • idea插件开发的第二天-写一个时间查看器
  • VMware时提示系统尚未修改安装失败