gpt4 book ai didi

c++ - thead joinable-join 可以有竞争条件吗?你怎么绕过它?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:07 25 4
gpt4 key购买 nike

假设我有以下类(class)

 class A
{
public:
A()
{
my_thread=std::thread(std::bind(&A::foo, this));
}
~A()
{
if (my_thread.joinable())
{
my_thread.join();
}
}
private:
std::thread my_thread;
int foo();
};

基本上,如果我的线程在 joinable 和 join 调用之间完成,那么 my_thread.join 会永远等待吗?你如何解决这个问题?

最佳答案

Basically, if my thread completes between the joinable and join calls, then my_thread.join will wait forever?

没有。线程完成后仍然可以连接;它只有在加入或分离后才变得不可加入。

在控制 thread 对象被销毁之前,必须加入或分离所有线程。

关于c++ - thead joinable-join 可以有竞争条件吗?你怎么绕过它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22843655/

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