当前位置:网站首页>Thread learning notes
Thread learning notes
2022-07-22 18:22:00 【Lewin~】
One 、 Thread overview
1.1 The concept of thread
Each process has its own address space , This causes the process to create 、 Switch 、 When revoked , It requires a lot of overhead . To reduce system overhead , Threads evolved from processes . Threads exist in the process , Sharing process resources . A thread is an independent flow of control in a process , By environment ( Including register group and program counter ) And a series of execution instructions . Each process has an address space and a control thread .
1.2 Comparison between thread and process
Thread is CPU Basic unit of dispatch and dispatch , Process is the basic unit of program execution and resource allocation in a system , A process is a container for threads , Generally, threads can be regarded as lightweight processes
A process can create multiple threads , Multiple threads share the resources of a process
When each process is created, the system will give it 4G Virtual memory ,3G User space is private , So when the process switches , User space will also switch , So it will increase the system overhead , And multiple threads in a process share the resources of a process , So there is no need to switch these resources when switching threads , More efficient
The scheduling mechanism of threads is the same as that of processes , Multiple threads switch back and forth , Time slice rotation , Context switch
Two 、 Thread related API
Include header file :#include <pthread.h>
Create child threads : pthread_create()
Wait for child thread to exit : pthread_join()
Thread separation :pthread_detach()
Thread to exit :pthread_exit()
Cancellation of thread : pthread_cancel()
Set whether the thread can be canceled :pthread_setcancelstate()
Set the cancellation point of the thread :pthread_testcancel()
Set whether to cancel the thread immediately or after running to the cancellation point : pthread_setcanceltype()
The registration thread exits the cleanup function :pthread_cleanup_push()
Run the thread cleanup function and pop up the stack : pthread_cleanup_pop()
3、 ... and 、 matters needing attention
And fork The difference is pthread_create The created thread does not start running at the same point as the parent thread , Instead, run from the specified function , After this function runs , The thread also exits .
Thread depends on the existence of process , If the thread creation process is over , The thread is over .
The program of thread function is in pthread In the library , So when you link, you need to add parameters -lpthread
linux Thread execution and windows Different ,pthread There are two states :
Combinable (joinable) Or separate (detached), Threads are created as associative by default .
(1) If the thread is joinable state , When the thread function returns and exits itself pthread_exit The stack and thread descriptor occupied by the thread will not be released ( A total of 8K many ). Only when you call pthread_join Then these resources will be released .
(2) if detached Thread in state , These resources are used when the thread function exits pthread_exit It will be released automatically , Use pthread_detach Function to set the thread to separate state .
(3) After a thread is created, its resources should be recycled , But use pthread_join The function blocks the caller , so Linux Provides thread separation functions :pthread_detachpthread_cancel The essence of a function is to send a signal to the target thread thread, But sending successfully does not mean that the target thread will terminate , Depends on the cancellation property of the thread
It is the same as the exit cleanup of the process , A thread can also register the function it will call when it exits , Such a function is called a thread cleanup handler (thread cleanup handler).
(1) Be careful : Threads can build multiple cleanup handlers . The handler is on the stack , Therefore, their execution order is opposite to that when they are registered .
(2) The cleanup function will be called when the thread performs the following actions :
1、 call pthread_exit Exit thread . 2、 Respond to cancellation requests from other threads .
3、 Use nonzero execute call pthread_cleanup_pop
边栏推荐
猜你喜欢
随机推荐
【How To 系列】好友裂变平台搭建
A survey of network defense decision-making methods based on attack defense game
Gbase8s database restrictions on set collection
基于流谱理论的SSL/TLS协议攻击检测方法
Microsoft SQL Server数据库语言及功能使用(十二)
prosody相关概念了解。xmpp,jabber,bosh等
How to solve the gloomy life under the middle-aged crisis of it
GBase8sUNION ALL 运算符
Research on network slicing security for 5g mmtc
Hblock盘活企业级存储市场
What level do programmers need to reach to get 20K monthly salary without pressure?
「武汉理工大学 软件工程复习」第五章 | 软件体系结构
GBase8s数据库由数据库对象执行的对照
SQL多条件查询无法实现
测试员职场最大的危机不是30岁,而是中年被裁
Web3 sharing
"Review of software engineering in Wuhan University of technology" Chapter 2 | software process model
QT notes - qudpsocket of network communication
关于STemwin中,外部实体按键操作Spinbox控件(fishing_2)
一线互联网P7面试题总结---补充中