问题描述
我正在尝试从线程本身调用 std::thread::joinable()
。然而,即使线程应该处于活动状态,返回值在开始时也是假的。例如下面的一段代码:
#include <iostream>
int main()
{
std::thread thethread;
int a = 0;
auto thefun = [&]() {
a++;
a++;
std::cout << thethread.joinable() << std::endl;
std::cout << thethread.joinable() << std::endl;
std::cout << "msg2 from thefun" << std::endl;
std::cout << thethread.joinable() << std::endl;
};
thethread = move(std::thread(thefun));
thethread.join();
return 0;
}
输出:
0
1
msg from thefun
1
我不明白为什么对 thethread.joinable()
的第一次调用返回 false 而当调用 std::cout
时它开始返回 true。我有一种感觉,这是由我的愚蠢错误引起的,我希望有人能指教我。也许我不应该在线程本身中调用 joinable()
?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)