当前位置:网站首页>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
边栏推荐
- It is difficult to quantify the extent to which a single-chip computer can find a job
- STM32 interrupt priority management
- STM32 general timer output PWM control steering gear
- Gpiof6, 7, 8 configuration
- There is no specific definition of embedded system
- Not many people can finally bring their interests to college graduation
- Raspberry pie installation SciPy
- Adaptiveavgpool1d internal implementation
- I didn't think so much when I was in the field of single chip microcomputer. I just wanted to earn money to support myself first
- 2021-10-27
猜你喜欢

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

Stm32-hal library learning, using cubemx to generate program framework

Oracle database SQL statement execution plan, statement tracking and optimization instance

嵌入式系统没有特别明确的定义

Hal library sets STM32 clock

Basic knowledge of communication interface

An executable binary file contains more than machine instructions

The third paper of information system project manager in soft examination

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers

There is no specific definition of embedded system
随机推荐
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
My notes on intelligent charging pile development (II): overview of system hardware circuit design
万字手撕七大排序(代码+动图演示)
[keil5 debugging] warning:enumerated type mixed with other type
QT qcombobox QSS style settings
使用sed替换文件夹下文件
Interruption system of 51 single chip microcomputer
Not many people can finally bring their interests to college graduation
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
Project cost management__ Cost management technology__ Article 6 prediction
STM32 external interrupt experiment
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Quelle langue choisir pour programmer un micro - ordinateur à puce unique
Drive and control program of Dianchuan charging board for charging pile design
STM32 interrupt switch
对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
The 4G module designed by the charging pile obtains NTP time through mqtt based on 4G network
单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇