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

线程ID和线程库

在linux中,线程的运行可以用lwp来标识,只是操作系统的标识方法,lwp表示轻量级进程,在Linux中,进程和线程都可以用lwp来标识,而对于用户来说,也有对应的线程ID,

线程库

在linux中,运行线程时,要链接上线程库,而线程ID就是线程库用来标识线程的,

当我们执行一个进程时,要把代码和数据从磁盘中加载到内存中,建立task_struct,当我们要创建线程时,要把动态库加载到内存中,然后通过页表映射到虚拟地址空间中,

关于线程的管理,也是在线程库中进行管理的,每创建一个线程,就会有一个管理线程的strcut tcb,其中包含线程ID,线程局部储存,线程栈等等,本质上,它就是内存上的·一段空间,线程ID本质上就是虚拟地址,

___thread关键字

__thread 是 GCC 提供的一个关键字,用于声明线程局部存储(Thread-Local Storage,TLS)。使用 __thread 声明的变量会为每个线程创建一个独立的实例,这意味着每个线程访问的都是自己的变量副本,而不是共享的全局变量。

比如说

__thread int thread_var=10;

thread_var的作用域是全局,但是每个线程都会单独创建一个thread_var的局部变量,与全局的thread_var不冲突

线程的封装

#pragma once
#include <iostream>
#include <string>
#include <pthread.h>namespace Thread_ljp
{typedef void (*func_t)(const std::string &name);class Thread{public:void Excute(){std::cout<<_name<<" is running"<<std::endl;_isrunning=true;_func(_name);_isrunning=false;}public:Thread(const std::string& name,func_t func):_name(name),_func(func){std::cout<<"creat "<<name<<" done"<<std::endl;}static void* ThreadRoutine(void* args){Thread* self=static_cast<Thread*>(args);self->Excute();return nullptr;}bool Start(){int n=::pthread_create(&_tid,nullptr,ThreadRoutine,this);if(n!=0)return false;return true;}std::string Status(){if(_isrunning)return "running";return "sleep";}void Stop(){if(_isrunning){::pthread_cancel(_tid);_isrunning=false;std::cout<<_name<<"stop"<<std::endl;}}void Join(){::pthread_join(_tid,nullptr);std::cout<<_name<<" Joined"<<std::endl;}std::string Name(){return _name;}~Thread(){}private:std::string _name;pthread_t _tid;bool _isrunning;func_t _func;};
}

#include <iostream>
#include <vector>
#include <cstdio>
#include <unistd.h>
#include "Thread.hpp"using namespace Thread_ljp;void Print(const std::string &name)
{int cnt = 1;while (true){std::cout << name << "is running, cnt: " << cnt++ << std::endl;sleep(1);}
}
const int gnum = 10;
int main()
{// 管理原生线程std::vector<Thread> threads;for (int i = 0; i < gnum; i++){std::string name = "thread-" + std::to_string(i + 1);threads.emplace_back(name, Print);sleep(1);}// 启动线程for (auto &thread : threads){thread.Start();}sleep(10);for (auto &thread : threads){thread.Stop();}for (auto &thread : threads){thread.Join();}return 0;
}

http://www.lryc.cn/news/476830.html

相关文章:

  • 使用AWS Lambda构建无服务器应用程序
  • 响应式网页设计案例
  • 麦麦Docker笔记(一)
  • 【设计模式系列】总览
  • P11118 [ROI 2024 Day 2] 无人机比赛 题解
  • 时序数据库是什么:概念、特点与分类简析
  • 大数据上岗.入职.就业面试题
  • 2016年7月和8月NASA的气候成像(ATom)-1飞行活动期间测量的黑碳(BC)质量混合比(单位为ng BC / kg空气)
  • python opencv3
  • git原理与上传
  • LeetCode:633. 平方数之和(Java)
  • linux查看端口状态的命令合集
  • 幼儿园篮球游戏
  • Android编译环境构建(二)(可用于物理机、虚拟机、容器化Jenkins环境)
  • Web服务器(实验)
  • 【湖南-常德】《市级信息化建设项目初步设计方案编制规范和支出预算编制标准(试行)》-省市费用标准解读系列05
  • 微信小程序 https://pcapi-xiaotuxian-front-devtest.itheima.net 不在以下 request 合法域名
  • vue什么时候渲染旧的VDOM,什么时候渲染新的VDOM
  • 【Qwen2技术报告分析】从模型架构 数据构建和模型评估出发
  • Naive UI 选择器 Select 的:render-option怎么使用(Vue3 + TS)(鼠标悬停该条数据的时候展示全部内容)
  • 使用Mac如何才能提高OCR与翻译的效率
  • QML----复制指定下标的ListModel数据
  • CSS Text(文本)
  • 聊一聊Spring中的@Transactional注解【下】【注解失效场景】
  • 对称加密与非堆成加密
  • 江协科技STM32学习- P28 USART串口数据包
  • Linux脚本循环(for、while、until)
  • 文件系统上云的挑战
  • 【北京迅为】《STM32MP157开发板嵌入式开发指南》-第七十一章 制作Ubuntu文件系统
  • 中间件漏洞总结