gpt4 book ai didi

c++ - 分离线程执行结束

转载 作者:行者123 更新时间:2023-11-28 05:33:19 25 4
gpt4 key购买 nike

我目前正在创建一个从 Java 环境 (NDK) 中调用 native 代码的程序。我有一个函数,将从我的 Java 代码中调用几次。在此函数中,我创建了一个分离线程,它将充当计时器以停止我的 native 代码中的特定函数。代码如下所示:

void myNativeFunction(){
std::thread timerTrial(&FluidMechanics::Impl::endTrial,this);
timerTrial.detach();
}

void endTrial(){
//code here
return ;
}

问题是,当 endTrial 返回时,我想知道当我返回时我的线程是否会被终止并释放所有资源。事实上,在那之后不久,myNativeFunction 将再次被调用几次,所以我不想弄乱我的资源和内存。

最佳答案

是的。当分离的线程返回时,任何线程特定的资源都将被自动释放。

C++11 草案,N3690,§ 30.3.1.7, 10, detach()

The thread represented by *this continues execution without the calling thread blocking. When detach() returns, *this no longer represents the possibly continuing thread of execution. When the thread previously represented by *this ends execution, the implementation shall release any owned resources.

(强调我的)。

关于c++ - 分离线程执行结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com