线程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;
}