当前位置:网站首页>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
边栏推荐
- Eight working modes of stm32gpio and chip naming rules
- 单片机学到什么程度能找到工作,这个标准不好量化
- 03 FastJson 解决循环引用
- Design of charging pile mqtt transplantation based on 4G EC20 module
- STM32 general timer output PWM control steering gear
- Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
- Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
- yocto 技术分享第四期:自定义增加软件包支持
- Oracle database SQL statement execution plan, statement tracking and optimization instance
- Education is a pass and ticket. With it, you can step into a higher-level environment
猜你喜欢
It is difficult to quantify the extent to which a single-chip computer can find a job
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
Crash工具基本使用及实战分享
Oracle database SQL statement execution plan, statement tracking and optimization instance
学习开发没有捷径,也几乎不存在带路会学的快一些的情况
An executable binary file contains more than machine instructions
Serial communication based on 51 single chip microcomputer
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
My notes on intelligent charging pile development (II): overview of system hardware circuit design
随机推荐
Idea remote breakpoint debugging jar package project
4G module IMEI of charging pile design
Assignment to '*' form incompatible pointer type 'linkstack' {aka '*'} problem solving
开学实验里要用到mysql,忘记基本的select语句怎么玩啦?补救来啦~
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Design of charging pile mqtt transplantation based on 4G EC20 module
内存数据库究竟是如何发挥内存优势的?
我想各位朋友都应该知道学习的基本规律就是:从易到难
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
I think all friends should know that the basic law of learning is: from easy to difficult
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
Yocto Technology Sharing Phase 4: Custom add package support
Runtime. getRuntime(). GC () and runtime getRuntime(). The difference between runfinalization()
Synchronization control between tasks
Gpiof6, 7, 8 configuration
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Swing transformer details-1
yocto 技术分享第四期:自定义增加软件包支持
4G module board level control interface designed by charging pile