当前位置:网站首页>2021-11-11 standard thread library
2021-11-11 standard thread library
2022-07-03 10:02:00 【Master Hao】
Who called join() function ?d2 This thread object calls join() function , So we have to wait d2 The download task of is over ,d2.join() Function to get the return .
d2 In which thread environment join() function ?d2 It is called in the context of the main thread join() function , So the main thread has to wait d2 The thread work of is finished , Otherwise, the main thread will always be block state ; What should not be confused here is d2 Real tasks ( download ) It is done in another thread , however d2 call join() The action of the function is done in the main thread environment .
detach: Detach Threads . Separate the execution instance represented by the current thread object from the thread object , So that the thread can be executed independently . Once the thread is finished , The resources it allocates will be released .
call detach After the function :
*this No longer represents any thread execution instance .
joinable() == false
get_id() == std::thread::id()
But it will lose the opportunity to gain control through line-to-line .
In the use of std::thread When , There are two operations on the created thread : wait for / Separate , That is to say join/detach operation .join() The operation is in std::thread t(func) after “ Some ” Call... Where appropriate , Its function is to reclaim the resources of the corresponding thread , Avoid resource leakage .detach() The operation is in std::thread t(func) Call immediately after , It is used to separate the created thread from the thread doing the creation action , The detached thread becomes a background thread , then , The number of threads created “ Life and death ” It has nothing to do with the thread that does the creation action , Its resources will be init Process recycling .
join Not in tune , Cause memory leaks , It is better to bind a destructor .
When calling detach or join When the method is used ,thread The state of the object will be determined by joinable To non-joinable, That is to say std::thread::joinable() The return value of will be true Turn into false
stay thread When the object is deconstructed , Will judge joinable state , If the current object is joinable Of , Will call std::terminate(), The end of the process .
because thread Object is out of scope and destructed , But the thread is still , So an exception occurred .
In the following code , Called t.detach(), stay t When leaving scope destruct , Will not end the process .
#include
#include
#include
void independentThread()
{
std::cout << “Starting concurrent thread.\n”;
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << “Exiting concurrent thread.\n”;
}
int main()
{
{
std::thread t(independentThread);
t.detach();
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << “Exiting main thread.\n”;
C. call new Created std::thread, Direct use of delete, Need to call delete Before , call join() perhaps detach()
If either of these two functions is called immediately after the thread is created , You can no longer call , But to be on the safe side , Need to add if(joinable()) The judgment of the , See the previous article for the reason
边栏推荐
- STM32 interrupt switch
- Exception handling of arm
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- 2. Elment UI date selector formatting problem
- Synchronization control between tasks
- 手机都算是单片机的一种,只不过它用的硬件不是51的芯片
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
- (2)接口中新增的方法
- 01仿B站项目业务架构
- Education is a pass and ticket. With it, you can step into a higher-level environment
猜你喜欢

Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit

Quelle langue choisir pour programmer un micro - ordinateur à puce unique

开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~

Not many people can finally bring their interests to college graduation

03 fastjason solves circular references

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

uniapp 实现微信小程序全局分享及自定义分享按钮样式

Mysql database underlying foundation column

学历是一张通行证,门票,你有了它,可以踏入更高层次的环境里

STM32 serial communication principle
随机推荐
Pymssql controls SQL for Chinese queries
Project cost management__ Cost management technology__ Article 7 completion performance index (tcpi)
Oracle database SQL statement execution plan, statement tracking and optimization instance
In third tier cities and counties, it is difficult to get 10K after graduation
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
03 fastjason solves circular references
GPIO port details, Hal library operation keys
Not many people can finally bring their interests to college graduation
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688
Stm32f407 key interrupt
Screen display of charging pile design -- led driver ta6932
Serial communication based on 51 single chip microcomputer
03 FastJson 解决循环引用
51 MCU tmod and timer configuration
手机都算是单片机的一种,只不过它用的硬件不是51的芯片
Schematic diagram and connection method of six pin self-locking switch
My notes on intelligent charging pile development (II): overview of system hardware circuit design
[keil5 debugging] warning:enumerated type mixed with other type
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip