C++11 Thread::join (4)

Source: Internet
Author: User

Original address: http://www.cplusplus.com/reference/thread/thread/join/Public member function
<thread> std::thread::join
void join ();
Join ThreadThe function returns when the thread execution have completed.

Returned when the thread finishes executing. (that is, wait for the child thread to finish before proceeding with the main thread)


This synchronizes the moment this function returns with the completion of the operations in the thread:this block s the execution of the thread that calls this function until the function called on construction returns (if it hasn ' t yet ).

The function's return is synchronized with the child thread execution, which blocks the thread that called the function until the child thread has finished calling.

Example:

#include <iostream> #include <thread> #include <vector> #include <ctime>using namespace std;// Delay (n) delay n seconds  void Delay (double sec)  {      time_t start_time, cur_time;//variable declaration time      (&start_time);      Do {time          (&cur_time);          } while ((Cur_time-start_time) < sec);  };  void Show (int n) {while (n>5) {cout<< "CurrentThread are" <<pthread_self () << ", now N is" <<n< <endl;delay (1); n--;}} int main () {cout<< "main starts" <<endl;thread T2 (show,10);//t2.join ();cout<< "main complete!" <<endl;}
Run:


As you can see, the T2 is over before it is finished.

Plus T2.join () after the execution result:


As you can see, the main thread is blocked, waiting for T2 to finish executing before proceeding to the main threads.


After a call to this function, the thread object becomes non- joinable and can is destroyed safely.

After the function is called, the child thread object becomes non-joinable and can be safely destroyed.


ParametersNone

Return ValueNone

Example
12345678910111213141516171819202122232425
//Example for Thread::join#include <iostream>//Std::cout#include <thread>//Std::thread, Std::this_thread::sleep_for#include <chrono>//Std::chrono::seconds voidPause_thread (intN) {std::this_thread::sleep_for (Std::chrono::seconds (n)); Std::cout <<"pause of "<< N <<" seconds ended\n";}intMain () {std::cout <<"Spawning 3 threads...\n";  Std::thread T1 (pause_thread,1);  Std::thread T2 (pause_thread,2);  std::thread t3 (pause_thread,3); Std::cout <<"Done spawning threads. Now waiting for them to join:\n";  T1.join ();  T2.join ();  T3.join (); Std::cout <<"All threads joined!\n";return0;}
Edit & Run


Output (after 3 seconds):
Spawning 3 threads...Done spawning threads. Now waiting for them to join:pause of 1 seconds endedpause of 2 seconds endedpause of 3 seconds endedAll threads joined!

Data RacesThe object is modified.
Note that any operations on the thread object itself is not synchronized (unlike the operations within the thread it repre sents).

Exception Safety Basic Guarantee: If an exception are thrown by this member function, the object is left in thread 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 was the same as the thread attempted to join, or
-A deadlock is detected (implementations may detect certain cases of deadlock).

Note if the thread represented by the object terminates with a uncaught exception, this cannot is caught by the Curr ENT thread, and is terminate() automatically called.


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

Write the wrong or bad place please a lot of guidance, you can leave a message or click on the top left email address to send me an e-mail, point out my errors and shortcomings, so that I modify, better share to everyone, thank you.

Reprint Please specify source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email:[email protected]

2014-9-4

From Gdut

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




C++11 Thread::join (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.