简体   繁体   English

在Closuer join()线程C ++之前更新joinable()

[英]Update joinable() before closuer join() thread C++

How to manage thread like this: 如何管理这样的线程:

thread th(foo);

while(th.joinable())
{
  // thread foo is active;
  // some printing from foo function for example
}

th.join();

This while is infinite because th.joinable() is always true and not updating. while是无限的,因为th.joinable()始终为真且不更新。 What reason and how to solve this problem? 什么原因以及如何解决这个问题?

As Joachim pointed out, joinable() is true as long the thread is not already joined (or default constructed). 正如Joachim所指出的,只要线程尚未加入(或默认构造),joinable()就成立。

As I understand your requirements (from your comments), you may just call th.join() without the while-loop. 据我所知(从您的评论中)您的要求,您可以仅调用th.join()而无需while循环。 This blocks until foo is finished. 这将阻塞直到foo完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM