简体   繁体   English

C++ 中的可连接线程

[英]Joinable thread in C++

http://www.cplusplus.com/reference/thread/thread/joinable/ http://www.cplusplus.com/reference/thread/thread/joinable/

A thread object is joinable if it represents a thread of execution .如果线程 object 表示执行线程,则它是可连接的。

A thread object is not joinable in any of these cases:在这些情况下,线程 object 是不可连接的:

 if it was **default-constructed**. if it has been **moved from** (either constructing another thread object, or assigning to it). if either of its members join or detach has been called.
  • What is the meaning of default constructed, here?这里的 default constructed 是什么意思?

  • W.R.T moved from - When we have to put threads in a vector, we may create thread objects outside and then move them in the vector. W.R.T 移自 - 当我们必须将线程放入向量中时,我们可能会在外部创建线程对象,然后将它们移动到向量中。 Is this one of the cases which this moved from is referring to?这是 this moved from 所指的案例之一吗?

  • After detach has been called once, can we never join it again? detach调用一次后,是不是就不能再join了呢?

What is the meaning of default constructed, here?这里的 default constructed 是什么意思?

It means a std::thread which was constructed with no arguments, and therefore does not represent a thread (ie it is not "running").它意味着一个 std::thread 没有 arguments 构造,因此不代表一个线程(即它不是“运行中的”)。 See docs: https://en.cppreference.com/w/cpp/thread/thread/thread请参阅文档: https://en.cppreference.com/w/cpp/thread/thread/thread

W.R.T moved from - When we have to put threads in a vector, we may create thread objects outside and then move them in the vector. W.R.T 移自 - 当我们必须将线程放入向量中时,我们可能会在外部创建线程对象,然后将它们移动到向量中。 Is this one of the cases which this moved from is referring to?这是 this moved from 所指的案例之一吗?

Yes, you cannot join a thread after moving it into the vector.是的,您不能在将线程移入向量后加入线程。 But you can of course join the new thread object inside the vector (where it was "moved to").但是您当然可以在向量中加入新线程 object(它被“移动到”的位置)。

After detach has been called once, can we never join it again? detach调用一次后,是不是就不能再join了呢?

That's right, you cannot join a thread which has already been joined or detached.没错,您不能加入已经加入或分离的线程。

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

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