当前位置:网站首页>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
边栏推荐
- 自動裝箱與拆箱了解嗎?原理是什麼?
- All processes of top ten management in project management
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- (1) What is a lambda expression
- 01 business structure of imitation station B project
- Stm32 NVIC interrupt priority management
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- ADS simulation design of class AB RF power amplifier
- Mysql database underlying foundation column
- Interruption system of 51 single chip microcomputer
猜你喜欢
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Project cost management__ Topic of comprehensive calculation
Project cost management__ Cost management technology__ Article 6 prediction
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
STM32 serial port usart1 routine
Stm32-hal library learning, using cubemx to generate program framework
CEF download, compile project
There is no specific definition of embedded system
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
万字手撕七大排序(代码+动图演示)
随机推荐
Stm32-hal library learning, using cubemx to generate program framework
当你需要使用STM32某些功能,而51实现不了时, 那32自然不需要学
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
【力扣刷题笔记(二)】特别技巧,模块突破,45道经典题目分类总结,在不断巩固中精进
Blue Bridge Cup for migrant workers majoring in electronic information engineering
MySQL 数据库基础知识(系统化一篇入门)
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
el-table X轴方向(横向)滚动条默认滑到右边
4G module at command communication package interface designed by charging pile
is_ power_ of_ 2 judge whether it is a multiple of 2
Synchronization control between tasks
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
STM32 serial communication principle
要選擇那種語言為單片機編寫程序呢
In third tier cities and counties, it is difficult to get 10K after graduation
01 business structure of imitation station B project
万字手撕七大排序(代码+动图演示)
It is difficult to quantify the extent to which a single-chip computer can find a job
STM32 external interrupt experiment
Positive and negative sample division and architecture understanding in image classification and target detection