C++11 Thread::d Etach (2)

Source: Internet
Author: User

Original address: http://www.cplusplus.com/reference/thread/thread/detach/Public member function<thread> std::thread::d etach
void Detach ();
Detach Thread

Detaches the thread represented by the object from the calling thread, allowing them to execute independently from each OT Her.

detach this thread from the calling thread to allow this thread to execute independently. ( But when the main process is finished, even detach () out of the sub-thread will be forced to kill whether or not it is completed )

Example:

#include <iostream> #include <thread> #include <vector> #include <ctime> #include <fstream >using namespace Std;//delay (n) delay n sec  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) {ofstream fout ("Detach.txt"); if (!fout.is_open ()) cout<< "Open failed!" <<endl;while (n>0) {fout<< "1currentThread is" <<pthread_self () << ", now N is" <<n< <endl;delay (0.2); n--;} fout<< "OK" <<endl;fout.close ();} int main () {cout<< "main starts" <<endl;thread T (show,100); T.detach ();d elay (1);cout<< "main complete !" <<endl;}
Run:



can be seen, when the process is finished, even if detach does not complete the task, it will be forced to kill. .


Both threads continue without blocking nor synchronizing in any. Note that if either one ends execution, its resources is released.

Two threads do not clog or sync, and note that the resources held at the end of any one of them will be released.


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

After the method is called, the thread object becomes unreachable and can be safely destroyed.

Example:



ParametersNone

Return ValueNone

Example
1234567891011121314151617181920212223
#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 and detaching 3 threads...\n";  Std::thread (pause_thread,1). Detach ();  Std::thread (pause_thread,2). Detach ();  Std::thread (pause_thread,3). Detach (); Std::cout <<"Done spawning threads.\n"; Std::cout <<"(the main thread will now pause for 5 seconds)\n";//give the detached threads time to finish (and not guaranteed!):Pause_thread (5);return0;}
Edit & Run


Output (after 5 seconds):
Spawning and detaching 3 threads...Done spawning threads.(the main thread will now pause for 5 seconds)pause of 1 seconds endedpause of 2 seconds endedpause of 3 seconds endedpause of 5 seconds ended

Data RacesThe object is modified.

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


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

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-3

From Gdut

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






C++11 Thread::d Etach (2)

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.