C++11threadoperator=(7)


C++11threadoperator=(7)

原文地址:http://www.cplusplus.com/reference/thread/thread/operator=/
public member function
<thread>

std::thread::operator=

move (1)
thread& operator= (thread&& rhs) noexcept;
copy [deleted] (2)
thread& operator= (const thread&) = delete;
Move-assign thread

If the object is currently not joinable, it acquires the thread of execution represented by rhs (if
any).

如果对象现在是非joinable,将获取rhs的执行线程。

If it is joinableterminate() is
called.

如果是joinable,将调用terminate().


After the call, rhs no longer represents any thread of execution (as if default-constructed).

调用之后,rhs不再执行任何线程。


thread objects cannot be copied (2).

线程对象不能被复制。


Parameters

rhs

thread object whose state is moved to *this.

将被移动到现在线程的线程rhs.



Return value

*this
返回该线程。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// example for thread::operator=
#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
 
void pause_thread(int n) 
{
  std::this_thread::sleep_for (std::chrono::seconds(n));
  std::cout << "pause of " << n << " seconds ended\n";
}

int main() 
{
  std::thread threads[5];                         // default-constructed threads

  std::cout << "Spawning 5 threads...\n";
  for (int i=0; i<5; ++i)
    threads[i] = std::thread(pause_thread,i+1);   // move-assign threads

  std::cout << "Done spawning threads. Now waiting for them to join:\n";
  for (int i=0; i<5; ++i)
    threads[i].join();

  std::cout << "All threads joined!\n";

  return 0;
}

Output (after 5 seconds):


Spawning 5 threads...
Done spawning threads. Now waiting for them to join:
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
pause of 4 seconds ended
pause of 5 seconds ended
All threads joined!



Data races

Both rhs and the object are modified.


Exception safety

No-throw guarantee: never throws exceptions.


—————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-4

于GDUT

——————————————————————————————————————————————————————————————————




发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注