c++ thread join
- 原文地址:http://www.cplusplus.com/reference/thread/thread/join/ public member function
<thread>std::thread::join
Join thread The function returns when the thread execution has completed.1.voidjoin();
当该线程执行完成后才返回。(即等待子线程执行完毕才继续执行主线程)
This synchronizes the moment this function returns with the completion of all the operations in the thread: This blocks the execution of the thread that calls this function until the function called on construction returns (if it hasn't yet).
该函数的返回与子线程执行完毕同步,该函数会阻塞调用该函数的线程直到子线程调用完毕。
例子:
运行截图:01.#include <iostream>02.#include <thread>03.#include <vector>04.#include <ctime>05.using namespace std;06.//delay(n) 延时n秒07.voiddelay(doublesec)08.{09.time_t start_time, cur_time;// 变量声明10.time(&start_time);11.do{12.time(&cur_time);13.}while((cur_time - start_time) < sec );14.};15.voidshow(intn){16.while(n>5){17.cout<<"currentThread is "<<pthread_self()<<",Now n is "<<n<<endl;18.delay(1);19.n--;20.}21.}22.intmain()23.{24.cout<<"main starts"<<endl;25.thread t2(show,10);26.//t2.join();27.cout<<"main complete!"<<endl;28.}![\]()
可以看到,t2还没有执行完毕就已经结束了。加上t2.join()之后的执行结果:
![\]()
可以看到,阻塞了主线程,等待t2执行完毕才继续执行main线程。
After a call to this function, the thread object becomes non-joinable and can be destroyed safely.
调用该函数后,子线程对象变成non-joinable以及可以安全地销毁。
Parameters
none
Return value
none
Example
01.102.203.304.405.506.607.708.809.910.1011.1112.1213.1314.1415.1516.1617.1718.1819.1920.2021.2122.2223.2324.2425.25 |
01.<cite>// example for thread::join</cite>02.<dfn>#include <iostream> </dfn><cite>// std::cout</cite>03.<dfn>#include <thread> </dfn><cite>// std::thread, std::this_thread::sleep_for</cite>04.<dfn>#include <chrono> </dfn><cite>// std::chrono::seconds</cite>05. 06.<var>void</var> pause_thread(<var>int</var> n)07.{08.std::this_thread::sleep_for (std::chrono::seconds(n));09.std::cout << <kbd>"pause of "</kbd> << n << <kbd>" seconds ended10."</kbd>;11.}12. 13.<var>int</var> main()14.{15.std::cout << <kbd>"Spawning 3 threads...16."</kbd>;17.std::thread t1 (pause_thread,1);18.std::thread t2 (pause_thread,2);19.std::thread t3 (pause_thread,3);20.std::cout << <kbd>"Done spawning threads. Now waiting for them to join:21."</kbd>;22.t1.join();23.t2.join();24.t3.join();25.std::cout << <kbd>"All threads joined!26."</kbd>;27. 28.<var>return</var> 0;29.} |
Edit & Run |
Output (after 3 seconds):
1.<samp>Spawning 3 threads...2.Done spawning threads. Now waiting for them to join:3.pause of 1 seconds ended4.pause of 2 seconds ended5.pause of 3 seconds ended6.All threads joined!7.</samp> |
Data races
The object is modified.
Note that any operations on the thread object itself are not synchronized (unlike the operations within the thread it represents).
Exception safety
Basic guarantee: if an exception is thrown by this member function, the thread object is left in a valid state.
If the call fails, a system_error exception is thrown:
| exception type | error condition | description |
|---|---|---|
| system_error | errc::invalid_argument | - The thread object is not joinable |
| system_error | errc::no_such_process | - The thread object is not valid |
| system_error | errc::resource_deadlock_would_occur | - The current thread is the same as the thread attempted to join, or - A deadlock was detected (implementations may detect certain cases of deadlock). |
Note that if the thread represented by the object terminates with an uncaught exception, this cannot be caught by the current thread, and terminate() is automatically called.
—————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-4
于GDUT


浙公网安备 33010602011771号