Topic 1: How to start a thread

  1 #include <QCoreApplication>  2 #include <iostream>  3 #include <thread>  //Manage the class and functions of the thread  4   5 /*  6 * Topic 1: How to start  7  *  8 * When using the C ++ thread standard library,  9 * C ++ How to start a thread and change to how to construct a std :: thread object. 10  * 11 * Construct std :: thread has the following: 12 * 1. Incoming a function as a parameter 13 * 2. Incoming an invoking object 14 * 3. Incontinent LamDBA object 15 */ 16  17 /* 18 * Reflections: For a thread, how to give thread into the parameter, how to get return values ​​from thread 19 */ 20  21  22 /* 23 * Topic 2: After starting the thread, you need to be clearly the execution of the wait thread (add form ", or make it run (separated). 24  * 25 * If the std :: Thread object is not decided, the new thread will terminate. It is necessary to make sure the thread can be added correctly or separated. 26 * So, if the implementation of the new thread is not explicit, it may cause the new thread to run incomplete. 27  * 28 * std :: thread's designer sector will call std :: terminate () to terminate the operation of the new thread. 29  * 30 */ 31  32 void way_one(){//Topics 1 33     std::cout<<"way_one  hello word"<<std::endl; 34 } 35  36 class way_two//Topics 1 37 { 38 public: 39     way_two(){} 40     void operator()(){ 41         std::cout<<"way_two  hello word"<<std::endl; 42     } 43 }; 44  45 void way_threed(){//Topics 1 46     std::cout<<"way_threed  hello word"<<std::endl; 47 } 48  49 void way_four(){//Topics 2 50     std::cout<<"way_four  hello word"<<std::endl; 51 } 52  53  54 //int main(int argc, char *argv[]) 55 //{ 56 //    QCoreApplication a(argc, argv); 57  58 //    std::thread t_one(way_one); 59 //    t_one.join(); 60  61 //    way_two _way_two; 62 //    std::thread t_two(_way_two);  //Function object_way_two Copy to the memory space of the new thread, the calls and execution of the function object are in the memory space of the new thread. 63 //                                  //The copy of the function object should be consistent with the original function object, otherwise the result will be different from our expectations. 64 //    t_two.join(); 65  66 //// std :: thread _way_two_1 (Way_TWO ()); // Programmed Defining a Function Std :: Thread Func (Param); 67 //                                         //There is something to note that when the function object is incorporated into the thread constructor, it is necessary to avoid "the most human syntax" (C ++ 's MOST VEXING PARSE, 68 //                                         //Chinese profile). If you pass a temporary variable, not a named variable; the C ++ compiler will resolve it into a function declaration, not the definition of the type object. 69  70 //    std::thread _way_two_2((way_two()));  //Solution 1: Add a pair of brackets to temporary objects 71 //    _way_two_2.join(); 72  73 //    std::thread _way_two_3{way_two()};    //Solution 2: Initialization semantics using braces 74 //    _way_two_3.join(); 75  76 //    std::thread _way_three([]{ 77 //        way_threed(); 78 //    }); 79 //    _way_three.join(); 80  81 //    std::thread _way_four(way_four); //May be executed, or not do it, look at luck 82 //                                     //main () function execution ends, _way_four objects will be destructed, std :: thread destructure will call std :: terminal () terminate new threads. 83 //    return a.exec(); 84 //} 85  86 /* 87 * JOIN function: Initial thread is waiting for the new county execution to continue to implement it. 88  * 89 * Put all the JOINs of all JOINs to the main () function Return a.exec (), let's take a look. 90  * 91 * 2.1.2 Waiting for the thread 92 * Each time a std :: thread object is created, just use Join (), so the original thread does not do in its life cycle, 93 * Make the function with a separate thread becomes very small, 94 * But in actual programming, the original thread either has its own work; either start multiple sub-threads to do some useful work and wait for these threads to end. 95 * So, the process of the main () function above is obviously inappropriate, like the main () below, in the final use of Join (), 96 * Use std :: thread in the class to use JOIN () in the corresponding destructor. 97  * 98 * Join () is a simple and rude wait thread to complete or not wait. 99 * When you need to have more flexible control over the thread waiting, you will see if a thread ends.100 * Or only a period of time (more than time is determined as timeout). To do this, you need to use other mechanisms to do, such as conditional variables and expectations (FUTURES)101  *102  *103 * Call Join () behavior, also cleaned the thread-related storage section, so the std :: Thread object will no longer be associated with the already completed thread.104 * This means that only Join () can only be used for a thread; once you have used Join (), std :: thread object can not join again,105 * When using JoinAble (), false will be returned.106 */107 int main(int argc, char *argv[])108 {109     QCoreApplication a(argc, argv);110 111     std::thread t_one(way_one);112 113     way_two _way_two;114     std::thread t_two(_way_two);  //Function object_way_two Copy to the memory space of the new thread, the calls and execution of the function object are in the memory space of the new thread.115                                   //The copy of the function object should be consistent with the original function object, otherwise the result will be different from our expectations.116 117 //    std::thread _way_two_1(way_two()); //Programming Define a function std :: thread func (param);118                                          //There is something to note that when the function object is incorporated into the thread constructor, it is necessary to avoid "the most topic syntax parsing" (C ++ 's MOST VEXING PARSE,119                                          //Chinese profile). If you pass a temporary variable, not a named variable; the C ++ compiler will resolve it into a function declaration, not the definition of the type object.120 121     std::thread _way_two_2((way_two()));  //Solution 1: Add a pair of children with a temporary object122 123     std::thread _way_two_3{way_two()};    //​​Solution 2: Initialization semantics using braces124 125     std::thread _way_three([]{126         way_threed();127     });128 129 130     t_one.join();131     t_two.join();132     _way_two_2.join();133     _way_two_3.join();134     _way_three.join();135     return a.exec();136 }