目录
上面就创建了4个线程,其中每一个线程都是当前进程里面的一个执行流。
重新理解进程与线程:
进程:是上面图中框中的内容,包含task_struct,进程地址空间,页表,文件。信号等等,合起来称之为一个进程。站在内核角度来理解进程:承担分配系统资源的基本实体,叫做进程。
线程:是最小的基本调度单位,一个进程内的线程共享其中的资源。
其中,Linux系统中没有真正意义上的线程,而是用进程模拟的。
操作系统要支持真的线程,那么就需要对这些线程进行管理。比如说创建线程、终止线程、调度线程、切换线程、给线程分配资源、释放资源以及回收资源等,而在Linux看来,描述线程的控制块和描述进程的控制块是类似的,因此Linux并没有重新为线程设计数据结构,而是直接复用了进程控制块,所以我们说Linux中的所有执行流都叫做轻量级进程。没有真正的线程,那么也就绝对没有真正意义上的线程相关的系统调用!
但是Linux可以提供创建轻量级进程的接口,如vfork函数,原生线程库pthread
pid_t vfork(void);
返回值与fork函数相同
原生线程库:原生线程库实际就是对轻量级进程的系统调用进行了封装,在用户层模拟实现了一套线程相关的接口。
线程的优点:
因为是在同一个地址空间,因此所谓的代码段(Text Segment)、数据段(Data Segment)都是共享的:
除此之外,各线程还共享以下进程资源和环境:
文件描述符表。(进程打开一个文件后,其他线程也能够看到)
每种信号的处理方式。(SIG_IGN、SIG_DFL或者自定义的信号处理函数)
当前工作目录。(cwd)
用户ID和组ID。
pthread线程库是应用层的原生线程库:
创建线程的函数:pthread_create
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
参数介绍:
返回值:线程创建成功返回0,失败返回错误码。
一个程序启动时,就有一个进程被操作系统创建,与此同时一个线程也立刻运行,这个线程就叫做主线程。主线程调用pthread_create函数创建一个新线程,此后新线程就会跑去执行自己的新例程,而主线程则继续执行后续代码。
例如:
- #include
- using namespace std;
- #include
- #include
- void* rountine(void* argv)
- {
- while (true)
- {
- string s = (char *)argv;
- cout << s << " pid=" << getpid() << endl;
- sleep(1);
- }
- }
- int main()
- {
- pthread_t tid1;
- pthread_t tid2;
- pthread_t tid3;
-
- pthread_create(&tid1,nullptr,rountine,(void*)"thread1");
- pthread_create(&tid2,nullptr,rountine,(void*)"thread2");
- pthread_create(&tid3,nullptr,rountine,(void*)"thread3");
-
- while(true)
- {
- cout<<"mainthread running"<<"pid= "<<getpid()<
- sleep(1);
- }
- return 0;
- }
结果:

用ps -aL命令查看显示当前的轻量级进程。
- 默认情况下,不带
-L,看到的就是一个个的进程。 - 带
-L就可以查看到每个进程内的多个轻量级进程。
例如:

其中,LWP(Light Weight Process)就是轻量级进程的ID,可以看到显示的三个轻量级进程的PID是相同的,因为它们属于同一个进程。
注意: 在Linux中,应用层的线程与内核的LWP是对应的,实际上操作系统调度的时候采用的是LWP,而并非PID,只不过我们之前接触到的都是单线程进程,其PID和LWP是相等的,所以对于单线程进程来说,调度时采用PID和LWP是一样的。
获取线程ID:
- 创建线程时通过输出型参数获得。
- 通过调用pthread_self函数获得。
pthread_self函数:
pthread_t pthread_self(void);
例如:
- void *rountine(void *argv)
- {
- while (true)
- {
- string s = (char *)argv;
- cout << s << " pid=" << getpid() <<" "<<"tid="<<pthread_self()<< endl;
- sleep(1);
- }
- }
- int main()
- {
- pthread_t tid1;
- pthread_t tid2;
- pthread_t tid3;
-
- pthread_create(&tid1, nullptr, rountine, (void *)"thread1");
- pthread_create(&tid2, nullptr, rountine, (void *)"thread2");
- pthread_create(&tid3, nullptr, rountine, (void *)"thread3");
-
- while (true)
- {
- cout << "mainthread running"
- << " pid=" << getpid() << endl;
- cout<<"mainthread running"<<" "<<"tid1="<
" ""tid2="<" "<<"tid3="< - sleep(1);
- }
- return 0;
- }
结果:

可以看出主线程打印的tid与每个线程打印的tid是相同的。
注意: 用pthread_self函数获得的线程ID与内核的LWP的值是不相等的,pthread_self函数获得的是用户级原生线程库的线程ID,而LWP是内核的轻量级进程ID,它们之间是一对一的关系。
3.2线程等待
与进程类似,如果主线程不对新线程进行等待,那么这个新线程的资源也是不会被回收的。所以线程需要被等待,如果不等待会产生类似于“僵尸进程”的问题,也就是内存泄漏。
等待线程函数:pthread_join
int pthread_join(pthread_t thread, void **retval);
参数说明:
- thread:被等待线程的ID。
- retval:线程退出时的退出码信息。
返回值说明:
- 线程等待成功返回0,失败返回错误码。
注意:调用该函数的线程会将挂起等待,直到ID为thread的线程终止,thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的。
如果thread线程通过return返回,retval所指向的单元里存放的是thread线程函数的返回值。
如果thread线程被别的线程调用pthread_cancel异常终止掉,retval所指向的单元里存放的是常数PTHREAD_CANCELED。
如果thread线程是自己调用pthread_exit终止的,retval所指向的单元存放的是传给pthread_exit的参数。
如果对thread线程的终止状态不感兴趣,可以传NULL给retval参数。
3.2线程终止
- 从线程函数return。
- 线程可以自己调用pthread_exit函数终止自己。
- 一个线程可以调用pthread_cancel函数终止同一进程中的另一个线程。
例如:
- void *rountine(void *argv)
- {
- while (true)
- {
- string s = (char *)argv;
- cout << s << " pid=" << getpid() <<" "<<"tid="<<pthread_self()<< endl;
- sleep(1);
-
- //return (void*)1314;
- //pthread_exit((void*)1314);
- pthread_cancel(pthread_self());//退出码为-1;
- }
- }
举例子:
主线程运行3秒后,取消1,2号线程,3号线程运行5秒后自己退出。
- void *rountine(void *argv)
- {
- while (true)
- {
- string s = (char *)argv;
- cout << s << " pid=" << getpid() <<" "<<"tid="<<pthread_self()<< endl;
- sleep(1);
-
- }
- }
- void *rountine2(void *argv)
- {
- for(int i=0;i<5;i++)
- {
- string s = (char *)argv;
- cout << s << " pid=" << getpid() <<" "<<"tid="<<pthread_self()<< endl;
- sleep(1);
- }
- pthread_exit((void*)1314);
- }
- int main()
- {
- pthread_t tid1;
- pthread_t tid2;
- pthread_t tid3;
-
- pthread_create(&tid1, nullptr, rountine, (void *)"thread1");
- pthread_create(&tid2, nullptr, rountine, (void *)"thread2");
- pthread_create(&tid3, nullptr, rountine2, (void *)"thread3");
-
- int k=0;
- while (true)
- {
- k++;
- cout<<"k="<
- if(k==3)//主线程将线程1,2取消
- {
- pthread_cancel(tid1);
- pthread_cancel(tid2);
- }
- if(k==6)
- break;
- cout << "mainthread running"
- << " pid=" << getpid() << endl;
- cout<<"mainthread running"<<" "<<"tid1="<
" ""tid2="<" "<<"tid3="< - sleep(1);
- }
- void* p1,* p2,* p3;
- p1=p2=p3=nullptr;
- pthread_join(tid1,&p1);
- pthread_join(tid2,&p2);
- pthread_join(tid3,&p3);
- cout<<"tid1="<<(long long)p1<<" "<<"tid2="<<(long long)p2<<" "<<"tid3="<<(long long)p3<
-
- return 0;
- }
-

3.4分离线程
- 默认情况下,新创建的线程是joinable的,线程退出后,需要对其进行pthread_join操作,否则无法释放资源,从而造成系统泄漏。
- 如果不关心线程的返回值,join是一种负担,这个时候,我们可以告诉系统,当线程退出时,自动释放线程资源。
补充:
线程被分离后,只是当其退出时不需要pthread_join操作,其未退出前依旧使用该进程的资源,甚至这个线程崩溃了也会影响其他线程。
可以是线程组内其他线程对目标线程进行分离,也可以是线程自己分离。
joinable和分离是冲突的,一个线程不能既是joinable又是分离的。
线程分离函数:
int pthread_detach(pthread_t thread);
线程分离成功返回0,失败返回错误码。
四.线程ID及进程地址空间布局
1.pthread_create函数会产生一个线程ID,该值与pthread_self()函数产生的结果是一样的,但与内核中的LWP不同。
2.内核中的LWP属于进程调度的范畴,因为线程是轻量级进程,是操作系统调度器的最小单位,所以需要一个数值来唯一表示该线程。
3.pthread_create函数第一个参数指向一个虚拟内存单元,该内存单元的地址即为新创建线程的线程ID,这个ID属于NPTL线程库的范畴,线程库的后续操作就是根据该线程ID来操作线程的。
前面以介绍了Linux没有实现真正的线程,只提供LWP,也就意味着操作系统只需要对内核执行流LWP进行管理,而供用户使用的线程接口等其他数据,应该由线程库自己来管理。可以再来看下线程库。其实该库是一个动态库。
如图:

进程运行时动态库被加载到内存,然后通过页表映射到进程地址空间中的共享区,此时该进程内的所有线程都是能看到这个动态库的。每个线程都有自己私有的栈,其中主线程采用的栈是进程地址空间中原生的栈,而其余线程采用的栈就是在共享区中开辟的,线程的各种属性,还有自己的线程局部存储,当中包含了对应线程被切换时的上下文数据等就在该共享区的一块区域中,因此我们要找到一个用户级线程只需要找到该线程内存块的起始地址,然后就可以获取到该线程的各种信息。就可以认为该线程ID就是地址。

-
相关阅读:
【PI仿真笔记2-电容模型2】
静态路由 网络实验
算法复杂度
mysql入门与mongoDB入门
《我在美国学游戏设计》笔记
ElasticSearch--分片的路由原理
城镇供水产销差问题分析与对策
傻白入门芯片设计,IP, MCM, SiP, SoC 和 Chiplet的区别(二)
[Node] Node.js Webpack打包图片-Js-Vue
优化 cesium 界面广告牌(billboard)数据量大于 10w +时,地图加载缓慢、卡顿、加载完成后浏览器严重卡顿甚至崩溃问题
-
原文地址:https://blog.csdn.net/m0_64397669/article/details/127958402