当前位置:网站首页>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
边栏推荐
- Basic knowledge of MySQL database (an introduction to systematization)
- 01仿B站项目业务架构
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
- [keil5 debugging] warning:enumerated type mixed with other type
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
- 內存數據庫究竟是如何發揮內存優勢的?
- STM32 interrupt switch
- Stm32f407 key interrupt
- Liquid crystal display
猜你喜欢

单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇

How does the memory database give full play to the advantages of memory?

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

LeetCode - 673. 最长递增子序列的个数

El table X-axis direction (horizontal) scroll bar slides to the right by default

要選擇那種語言為單片機編寫程序呢

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

2. Elment UI date selector formatting problem

SCM is now overwhelming, a wide variety, so that developers are overwhelmed

JS基础-原型原型链和宏任务/微任务/事件机制
随机推荐
Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
Screen display of charging pile design -- led driver ta6932
2. Elment UI date selector formatting problem
Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
03 FastJson 解决循环引用
2021-10-27
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Project scope management__ Scope management plan and scope specification
Schematic diagram and connection method of six pin self-locking switch
Eight working modes of stm32gpio and chip naming rules
Working mode of 80C51 Serial Port
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
I think all friends should know that the basic law of learning is: from easy to difficult
Hal library sets STM32 clock
yocto 技術分享第四期:自定義增加軟件包支持
byte alignment
Crash工具基本使用及实战分享
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Development of intelligent charging pile (I): overview of the overall design of the system