Second, thread creation, end

First, use the function to create a thread

1、thread

The main thread from the main start, we have created a thread should run from the beginning of a function (initial function), the end of the function, the end of the thread;

The main thread is finished, on behalf of the whole process is finished;

If the child thread has not finished, in general, the sub-thread is forcibly terminated;

1 . #include <the iostream>
 2 . #include < String >
 . 3 . #include <the Thread> // thread 
4 . the using  namespace STD;
 . 5 .
6 . // thread creation, which is a function of the initial 
7 . void myPrint () {
 . 8 . << COUT " . 1 the begin " << endl;
 . 9 . << COUT " 2 End " << endl;
 10 . }
 11 .
12 . int main () {
 13 is. myobj the Thread (the myPrint ()); // create a thread, the thread has started the implementation of the 
14 . myobj.join (); // block the main thread, the main thread and other threads executing the child, then the main thread continue to go down 
15 .    // If you do not write join, may first main execution is over, the child thread has not been performed, the error will be 
16 . << COUT " main End " << endl;
 . 17 .    return  0 ;
 18 is . }

2、detach

Normally the main thread to wait for child threads executing the continuing, there are exceptions.

detach function: separation, the main thread without waiting for a child thread, the main thread you move along, I perform my child thread, not a convergence. (Generally do not detach)

The reason: to create a lot of sub-thread, the main thread waits for the child by one end of the thread is not very good, it is proposed detach.

Only a little detach this thread objects lost contact with the main thread, the child thread will reside in the background, the library c ++ run time of takeover, after the child threads, the runtime library is responsible for cleaning up the thread related resources (guardian thread).

. 1  . 1 . # The include <the iostream> 2 2 . # The include <Thread>  // thread   . 3 . 3 . The using namespace  STD;  
 . 4 . 4 .  
 . 5 . 5 . // thread creation, i.e. an initial function   . 6 . 6 . Void  myPrint () {  
 . 7 . 7 . COUT << " . 1 the begin "  << endl;  
 . 8 . 8 . COUT << " 2 End " << endl;  
 . 9 . 9 .}  
 10 10 .  
  
 
    
     11  11 . Int  main () {  
 12  12 the Thread myobj (the myPrint ());. // create a thread, the thread has started the implementation of the   
13  13 myobj.detach ();.  
 14  14 . Cout << " main End " << endl ;  
 15  15 .     return  0 ;  
 16  16 }.  

Once used detach, we can no longer join up.

3、joinable()

To determine whether you can successfully use the join or detach, returns true or false.

It may be used generally join or detach before use, to first determine in advance, if true, the use of true or detach; otherwise it can not use the join or detach function.

Second, create a thread with class

With a class object creates a thread, the object is actually being copied to the thread, the main thread After execution, the object of this class will be destroyed, but the copy of the object is still there, until the end of this sub-thread, as long as you are the class object there is no reference, pointer, it will not cause problems.

When there are a class object reference:

. 1 #include <the iostream>
 2 #include <Thread> // thread 
. 3  the using  namespace STD;
 . 4  
. 5  class the TA {
 . 6  public :
 . 7      int & m_i; // reference 
. 8      the TA ( int & I): m_i (I) {} // constructor reference I 
. 9      void  operator () () {// objects can be achieved when using the functions
 10          COUT << " m_i value " << m_i << endl;
 . 11      }
 12 is  };
 13 is  
14  int main ( ) {
15      int A = . 9 ;
 16      the TA TA (A); // reference to A 
. 17      Thread myObj (TA); // TA adjustable target, 
18 is      myobj.detach ();
 . 19      // use this time detach it, it is possible the main thread to finish execution, a destroyed because TA is a reference, which is pointing to a memory has been destroyed, to be wrong, can only use the Join 
20      cout << " main End " << endl;
 21      return  0 ;
 22 }

If the main thread has finished the first execution, a destroyed, TA member variable is a reference type, that is, the child thread using a pointer (a reference is a special pointer) has a recovery address, similar to wild pointers, to to be wrong, so do not use detach.

After changed join, analyze code: ta create objects:

  1. Create a class object ta, the constructor is performed;
  2. Create threads myobj, inside ta copy objects to the thread myobj, ta copy of the object constructor executes;
  3. Execution thread, the main thread waits for the child thread to finish;
  4. Myobj end sub-thread, copying objects ta performed destructor;
  5. The main thread continues to execute main end;
  6. Code finish the main function, the main thread is finished, the object destructor TA executed, releases the memory and variable a;

Third, the use lambda expressions

. 1 #include <the iostream>
 2 #include <Thread> // thread 
. 3  the using  namespace STD;
 . 4  
. 5  int main () {
 . 6      Auto MyThread = [] {
 . 7          COUT << " I am a child thread started " << endl;
 . 8          COUT << " end " << endl;
 . 9      };
 10      Thread myObj (MyThread);
 . 11      myobj.join ();
 12 is      COUT << " main end " << endl;
13     return 0;
14 }

 

TA constructor executes, creating a thread myobj, ta copied into this object, so this

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11227877.html
Recommended