初谈Linux多线程--线程控制
创始人
2024-09-26 14:52:35
0

在这里插入图片描述

文章目录

  • 线程的概述
    • 理解线程
    • Linux中的线程
    • 重新理解的进程
    • Windows的线程
    • 线程的优点
    • 线程的缺点
      • 理解线程调度成本低
    • 进程VS线程
  • 线程控制
    • 创建线程
    • 等待线程
    • 线程函数传参
    • 线程的返回值
      • 新线程的返回值
      • 新线程返回值错误
      • 返回值为类对象
    • 创建多线程
    • 线程的终止
    • 线程的分离pthread_detach

线程的概述

理解线程

线程:线程是在进程内部(PCB)运行,是CPU内部调度的基本单位。

在这里插入图片描述


Linux中的线程

在Linux中,线程执行的是进程代码的一部分,也就是说,线程是进程的实体,可以看作是进程内的一个执行单元,我们将这些不同的执行单元称之为轻量级进程,不同线程之间可以通过共享内存来进行通信。
在这里插入图片描述

这里可以举一个例子,要想有一个幸福的家庭,家庭成员就要执行好自己的事情,这样才能成就一个幸福的家庭。

PCB大部分属性都包含了线程的属性,复用PCB,用PCB统一表示执行流,这样线程就不需要给线程单独设计数据结构和调度算法。这样维护的数据结构和调度算法的代码就少。

在CPU看来,不需要区分task_struct是进程还是线程,都叫做执行流。Linux执行流都是轻量级进程。Linux使用进程模拟线程。


重新理解的进程

以前我们学习的进程=内核数据结构+进程的数据代码,这是我们之前理解的。以前我们强调的是代码的执行流,内部只有一个执行流,而现在我们的内部有多个执行流。

因此以内核观点,给进程重新下一个定义:承担分配系统资源的基本实体。


Windows的线程

操作系统设计一个线程,需要新建、创建、销毁、管理等,线程要不要和进程产生关系呢?
操作系统需要对线程进行管理,先描述(struct tcb),再组织。

struct tcb { 	//线程的id,优先级,状态,上下文,连接属性... } 

Windows提供了真实的线程控制块,不是复用PCB属性,这样维护的数据结构和调度算法的代码就高。


线程的优点

  • 创建一个新线程的代价要比创建一个新进程小得多
  • 与进程之间的切换相比,线程之间的切换需要操作系统做的工作要少很多
  • 线程占用的资源要比进程少很多
  • 能充分利用多处理器的可并行数量
  • 在等待慢速I/O操作结束的同时,程序可执行其他的计算任务
  • 计算密集型应用,为了能在多处理器系统上运行,将计算分解到多个线程中实现
  • I/O密集型应用,为了提高性能,将I/O操作重叠。线程可以同时等待不同的I/O操作

线程的缺点

  • 性能损失
    一个很少被外部事件阻塞的计算密集型线程往往无法与共它线程共享同一个处理器。如果计算密集型
    线程的数量比可用的处理器多,那么可能会有较大的性能损失,这里的性能损失指的是增加了额外的
    同步和调度开销,而可用的资源不变。

  • 健壮性降低
    编写多线程需要更全面更深入的考虑,在一个多线程程序里,因时间分配上的细微偏差或者因共享了
    不该共享的变量而造成不良影响的可能性是很大的,换句话说线程之间是缺乏保护的。

  • 缺乏访问控制
    进程是访问控制的基本粒度,在一个线程中调用某些OS函数会对整个进程造成影响。

  • 编程难度提高
    编写与调试一个多线程程序比单线程程序困难得多


理解线程调度成本低

线程在同一个进程内部共享相同的地址空间和大部分资源,因此在创建、销毁或者切换线程时,无需像进程那样复制和维护额外的资源。这减少了资源管理的开销。

硬件角度:线程在同一核心上连续执行时,由于其数据可能保持在该核心的缓存中,可以更高效地利用缓存,从而提高数据访问的速度。这可以减少因缓存未命中而引起的额外延迟,进而降低线程切换的成本。


进程VS线程

  • 进程是资源分配的基本单位
  • 线程是调度的基本单位
  • 线程共享进程数据,但也拥有自己的一部分数据:
    线程ID
    一组寄存器:硬件上下文数据,体现出线程可以动态运行
    :线程在运行的时候,本质是运行函数,会形成临时变量,临时变量会被保存在每个线程的栈区
    errno
    信号屏蔽字
    调度优先级

进程的多个线程共享 同一地址空间,因此Text Segment、Data Segment都是共享的,如果定义一个函数,在各线程
中都可以调用,如果定义一个全局变量,在各线程中都可以访问到,除此之外,各线程还共享以下进程资源和环境:
文件描述符表、每种信号的处理方式(SIG_ IGN、SIG_ DFL或者自定义的信号处理函数)、当前工作目录、用户id和组id。

进程和线程的关系:

在这里插入图片描述

线程控制

在Linux系统中没有线程,只有轻量级进程,这个轻量级进程实际上就是线程,因为没有单独设计TCB。因此Linux操作系统不会直接给我们提供线程的系统调用,只会提供创建轻量级进程的系统调用接口。Linux系统存在一个中间软件层,有一个pthred库,是自带的原生线程库,对该轻量级进程接口进行封装,按照线程的接口提供给用户。所以说,Linux是用户级线程,Windows是内核级线程。

创建线程

创建一个进程的函数接口:

#include   int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);   Compile and link with -pthread.  

在这里插入图片描述

参数:
thread:返回线程ID,是一个输出型参数
attr:设置线程的属性,attrNULL表示使用默认属性
start_routine:是个函数地址,线程启动后要执行的函数
arg:传给线程启动函数的参数

返回值:成功返回0;失败返回错误码,内容未定义。

等待线程

等待线程的函数接口:

#include   int pthread_join(pthread_t thread, void**retval);  Compile and link with -pthread.  

参数
thread:线程ID
value_ptr:它指向一个指针,后者指向线程的返回值
返回值:成功返回0;失败返回错误码

代码示例:

#include #include #include #include  void *threadRun(void *args) {     int cnt=10;     while(cnt)     {         std::cout<<"new thread run ...,cnt:"<     pthread_t tid;     int n=pthread_create(&tid,nullptr,threadRun,(void*)"thread 1");     if(n!=0)     {         std::cerr<<"create thread error"<         std::cout<<"main thread wait sucess"<

在这里插入图片描述

上述代码中,子线程完成之后,主线程才退出。

这里的tid是什么?
tid是线程库中的一个地址,是虚拟地址。
在这里插入图片描述

线程函数传参

主线程数据可传递给新线程:
代码演示:

#include #include #include #include  void *threadRun(void *args) {     //std::string name=(const char*)args;     int a=*(int*)args;     int cnt=10;     while(cnt)     {         std::cout<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     pthread_t tid;      int a=100;      int n=pthread_create(&tid,nullptr,threadRun,(void*)&a);     if(n!=0)     {         std::cerr<<"create thread error"<         std::cout<<"main thread wait sucess"<

在这里插入图片描述


这里传递参数可以是任意类型,可以传递类对象,在传递类对象时,传递的是地址。也就是说,我们可以传递多个参数。

代码示例:

#include #include #include #include  class ThreadData { public:     std::string name;     int num; };  void *threadRun(void *args) {     //std::string name=(const char*)args;     //int a=*(int*)args;     ThreadData *td=static_cast(args);//(ThreadData*)args       int cnt=10;     while(cnt)     {         std::cout<name<<" run ...,num is:"<num<<",cnt:"<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     pthread_t tid;      ThreadData td;     td.name="thread-1";     td.num=1;      int n=pthread_create(&tid,nullptr,threadRun,(void*)&td);     if(n!=0)     {         std::cerr<<"create thread error"<         std::cout<<"main thread wait sucess"<

在这里插入图片描述

上述做法不推荐,td在主线程中,破坏了主线程的完整性和独立性。td 是一个局部变量,其生命周期仅限于 main 函数。一旦 main 函数结束,td 将会被销毁,此时新线程仍然可能在尝试访问已经无效的内存,从而导致未定义行为。另外,如果此时还有一个线程,使用的也是td访问的也是主函数中td变量,那这两个线程中如果其中一个线程对td进行修改,那么就会影响另一个线程。

换一种做法:

在主线程中申请一个堆上的空间,把堆上的地址拷贝给线程,此时对空间就交给了新线程。如果还有第二个线程,那就重新new一个堆上的空间,交给新的线程。这样每一个线程都有属于自己的堆空间。另外,在线程内部处理完后需要delete

#include #include #include #include  class ThreadData { public:     std::string name;     int num; };  void *threadRun(void *args) {     //std::string name=(const char*)args;     //int a=*(int*)args;     ThreadData *td=static_cast(args);//(ThreadData*)args       int cnt=10;     while(cnt)     {         std::cout<name<<" run ...,num is:"<num<<",cnt:"<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     pthread_t tid;      ThreadData *td=new ThreadData(); //在堆上申请空间     td->name="thread-1";     td->num=1;      int n=pthread_create(&tid,nullptr,threadRun,td);     if(n!=0)     {         std::cerr<<"create thread error"<         std::cout<<"main thread wait sucess"<

在这里插入图片描述

线程的返回值

新线程的返回值

线程的返回值是void*,那么主线程的函数就必须使用void **的参数返回,int pthread_join(pthread_t thread, void**retval)void**retval是一个输出型参数,需要一个void*类型的变量即void* retvoid* ret是一个指针类型的变量,有对应的空间,在Linux中占8个字节,在传递的时候是传递&ret ,是将地址传过去。

主线程拿到新线程的退出信息:
在这里插入图片描述

在这里插入图片描述

代码:

#include #include #include #include  class ThreadData { public:     std::string name;     int num; };  void *threadRun(void *args) {     //std::string name=(const char*)args;     //int a=*(int*)args;     ThreadData *td=static_cast(args);//(ThreadData*)args       int cnt=10;     while(cnt)     {         std::cout<name<<" run ...,num is:"<num<<",cnt:"<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     pthread_t tid;      ThreadData *td=new ThreadData(); //在堆上申请空间     td->name="thread-1";     td->num=1;      int n=pthread_create(&tid,nullptr,threadRun,td);     if(n!=0)     {         std::cerr<<"create thread error"<         std::cout<<"main thread wait sucess,new thread exit code:"<<(uint64_t)code<

在这里插入图片描述

可以根据退出信息,就可以判断新线程是否完成对应的任务。

新线程返回值错误

在这里插入图片描述

上述代码故意让新线程出现野指针,是的新线程出现错误。

在这里插入图片描述
上述代码时主线程,新线程出错后让主线程等100s后再退出。

代码演示结果:

在这里插入图片描述

主线程没有等100s后退出,而是在新的进程异常后直接退出。

线程的返回值只有正确时的返回值,一旦出现异常,线程就会崩溃,线程出现异常就会发信号给进程,进程就会被杀掉,即使进程里面有多个线程,里面有一个线程出现错误,整个进程都会被杀掉。
因此线程的在退出的时候只需要考虑正确的返回,不考虑异常,一旦异常,整个进程都会崩溃,包括主线程。

返回值为类对象

主线程创建并启动了一个新的线程,通过 pthread_create 和 pthread_join 实现了线程的创建和等待。
在新线程中,通过 ThreadData 类传递了操作数,并在 threadRun 函数中计算了加法操作的结果。
最后,主线程将计算结果打印出来,展示了线程之间数据的传递和简单的同步操作。

#include #include #include #include  class ThreadData { public:     int Excute()     {         return x+y;     } public:     std::string name;     int x;     int y; };  class ThreadResult { public:     std::string print()     {         return std::to_string(x)+"+"+std::to_string(y)+"="+std::to_string(result);     } public:     int x;     int y;     int result; };  void *threadRun(void *args) {     //std::string name=(const char*)args;     //int a=*(int*)args;     ThreadData *td=static_cast(args);//(ThreadData*)args       ThreadResult *result=new ThreadResult();     int cnt=10;     while(cnt)     {         sleep(1);          std::cout<name<<" run ..."<<",cnt:"<result=td->Excute();         result->x=td->x;         result->y=td->y;         break;      }     delete td;     return (void*)result; }  std::string PrintToHex(pthread_t &tid) {     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     pthread_t tid;      ThreadData *td=new ThreadData(); //在堆上申请空间     td->name="thread-1";     td->x=10;     td->y=20;      int n=pthread_create(&tid,nullptr,threadRun,td);     if(n!=0)     {         std::cerr<<"create thread error"<         std::cout<<"main thread wait sucess,new thread exit code:"<print()<

在这里插入图片描述

创建多线程

#include #include #include #include #include  using namespace std;  const int num=10;  void *threadrun(void* args) {     string name=static_cast(args);     while(true)     {         cout<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     vector tids;      for(int i=0;i         pthread_t tid;         char *name=new char[128];         snprintf(name,128,"thread-%d",i+1);//名字         pthread_create(&tid,nullptr,threadrun,name); //传递线程的名字          //保存所有线程的id信息         tids.push_back(tid);     }      //等待     for(auto tid:tids)     {         void *name=nullptr;         pthread_join(tid,&name);         //cout<

在主函数中,首先创建一个 vector 容器 tids,用于存储所有线程的 ID。使用for循环创建num个线程。在第一个for循环中,配一个新的字符数组name来存储线程名字,使用 snprintf 将线程名字格式化为 thread-i 的形式,调用 pthread_create 函数创建线程,传递线程名字作为参数,将每个线程的 ID 存入 tids 向量。第二个for循环中,等待所有进程结束,使用 pthread_join 函数等待线程结束,获取线程返回的 name,并输出线程名字加上 “quit…”,删除线程名字的内存,以防止内存泄漏。

运行结果:
在这里插入图片描述

线程的终止

新的线程如何终止?函数return

主线程如何终止?
主线程对应的main函数结束,那么主线程结束,表示整个进程结束。此时如果还有新线程还没有结束,那么新线程就会被提前终止,因此有线程等待,是的主线程最后结束。

使用线程终止函数:

#include   void pthread_exit(void *retval);  Compile and link with -pthread.  

代码测试:

#include #include #include #include #include #include  using namespace std;  const int num=10;  void *threadrun(void* args) {     string name=static_cast(args);     while(true)     {         cout<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     vector tids;      for(int i=0;i         pthread_t tid;         char *name=new char[128];         snprintf(name,128,"thread-%d",i+1);//名字         pthread_create(&tid,nullptr,threadrun,name); //传递线程的名字          //保存所有线程的id信息         //tids.push_back(tid);         tids.emplace_back(tid);     }      //等待     for(auto tid:tids)     {         void *name=nullptr;         pthread_join(tid,&name);         //cout<

在这里插入图片描述


还有一个终止线程函数:

#include   int pthread_cancel(pthread_t thread);  Compile and link with -pthread.  

这个实际上是取消一个线程,发送一个取消请求给线程,一般都是使用主线程取消其他线程。

代码示例:

#include #include #include #include #include #include  using namespace std;  const int num=10;  void *threadrun(void* args) {     string name=static_cast(args);     while(true)     {         cout<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     vector tids;      for(int i=0;i         pthread_t tid;         char *name=new char[128];         snprintf(name,128,"thread-%d",i+1);//名字         pthread_create(&tid,nullptr,threadrun,name); //传递线程的名字          //保存所有线程的id信息         //tids.push_back(tid);         tids.emplace_back(tid);     }      sleep(5);     //等待     for(auto tid:tids)     {         pthread_cancel(tid);//取消         cout<<"cancel:"<

在这里插入图片描述

线程的分离pthread_detach

线程的分离函数接口:

#include   int pthread_detach(pthread_t thread);  Compile and link with -pthread.  

如果一个线程被创建,默认是需要joinable的,必须被join;如果一个线程被分离,线程的工作状态是分离状态,不能被join

进程分离:一个线程依旧属于线程,但是不需要被主线程等待。

#include #include #include #include #include #include  using namespace std;  const int num=10;  void *threadrun(void* args) {     pthread_detach(pthread_self());     string name=static_cast(args);     while(true)     {         cout<     char buffer[64];     snprintf(buffer,sizeof(buffer),"0x%lx",tid);     return buffer; }  int main() {     vector tids;      for(int i=0;i         pthread_t tid;         char *name=new char[128];         snprintf(name,128,"thread-%d",i+1);//名字         pthread_create(&tid,nullptr,threadrun,name); //传递线程的名字          //保存所有线程的id信息         //tids.push_back(tid);         tids.emplace_back(tid);     }      //等待     for(auto tid:tids)     {         void *result=nullptr;  //现成被取消,被取消的线程的返回结果是-1         int n=pthread_join(tid,&result);         //cout<

在这里插入图片描述

上述代码表示:主线程创建一个新线程,但是新的线程被分离了,没有被等待,因此n返回为22。.

如果被分离的线程出异常依然会影响进程,会导致进程直接崩溃。

在这里插入图片描述

相关内容

热门资讯

安卓系统安不了小红书吗,安卓系... 最近有个问题在朋友圈里闹得沸沸扬扬,那就是“安卓系统安不了小红书吗?”这事儿可真是让人一头雾水,今天...
安卓系统下的游戏,探索热门游戏... 你有没有发现,手机里的游戏越来越好玩了?尤其是安卓系统下的那些游戏,简直让人停不下来!今天,就让我带...
深度系统安装安卓工具,一步到位... 亲爱的读者,你是否曾梦想过拥有一台完全由自己组装的安卓系统?想象那是一种怎样的自由和掌控感!今天,就...
安卓系统的虚拟男友 你有没有想过,在繁忙的生活中,有个虚拟的男朋友陪伴,那会是怎样的体验呢?没错,就是安卓系统的虚拟男友...
一加电视安卓系统,打造流畅智能... 亲爱的读者们,你是否在寻找一款既能满足日常观影需求,又能畅享安卓系统乐趣的电视呢?今天,就让我带你深...
逍遥安卓怎么把系统,逍遥安卓系... 你有没有想过,你的安卓手机系统是不是该换换口味了?别看它现在运行得风生水起,但有时候,换一个全新的系...
安卓系统内存占用评测,深度解析... 你有没有发现,随着智能手机的普及,安卓系统已经成为了我们生活中不可或缺的一部分。但是,你是否曾好奇过...
优学派 安卓系统版本,版本升级... 你有没有发现,现在的学习设备越来越智能了?今天,我就要和你聊聊一款特别火的学习神器——优学派,还有它...
苹果系统变安卓app,探索跨平... 你知道吗?最近苹果系统上的一些热门应用竟然悄悄地变成了安卓版的APP!这可真是让人眼前一亮的大事呢!...
安卓系统调用播放录音,Andr... 你有没有想过,手机里那些美妙的录音,是怎么被我们轻松播放的呢?今天,就让我带你一探究竟,揭秘安卓系统...
安卓系统安装应用黑屏,安卓系统... 最近是不是你也遇到了安卓系统安装应用时突然黑屏的尴尬情况?这可真是让人头疼啊!别急,今天就来给你详细...
老版安卓操作系统,操作系统的发... 你有没有想过,手机里的那些老版安卓操作系统,它们就像是一段段尘封的记忆,承载着我们的青春和回忆呢?今...
安卓系统免费开发版,解锁无限创... 你有没有想过,为什么安卓系统那么受欢迎?你知道吗,其实安卓系统有一个特别的地方,那就是它的免费开发版...
安卓系统未来会不会,未知。 你有没有想过,那个陪伴我们手机生活的安卓系统,它的未来会怎样呢?想象每天早上醒来,手机屏幕上跳出的信...
安卓手机2.3系统root,解... 你有没有想过给你的安卓手机2.3系统来个“大变身”?没错,我要说的就是那个神秘的root操作!别小看...
安卓系统的双屏手机,安卓系统引... 你知道吗?最近手机界可是掀起了一股新潮流,那就是安卓系统的双屏手机。这种手机不仅外观独特,功能也相当...
安卓系统如何清理文件,高效释放... 手机里的文件越来越多,是不是感觉安卓系统就像一个杂乱无章的仓库?别急,今天就来教你怎么清理安卓系统里...
调试安卓系统c源码,基于C源码... 亲爱的技术爱好者们,你是否曾对安卓系统的神秘面纱感到好奇?想要一探究竟,亲手调试安卓系统的C源码?那...
安卓系统4.0的模拟,系统革新... 你有没有想过,如果手机里的安卓系统4.0能像真人一样,活灵活现地出现在你面前,那会是怎样的场景呢?今...
安卓系统大_删除哪里,删除哪些... 手机里的安卓系统是不是越来越臃肿了?是不是觉得运行速度越来越慢,存储空间也越来越紧张?别急,今天就来...