c ++ 11 concurrency and multithreading (Lecture)

#include <iostream>
#include <the Thread>
#include <String>
a using namespace std;
/ *
int main ()
{
    sample demonstrates thread running start and end of
    the program up and running, generating a process, the main thread of the process belongs to begin automatically run;

    // actually the main thread of execution, the main thread begins execution mian function , the function returns the thread execution ends.
    << COUT "the Hello World!" << endl;
    return 0;
}
* /


// create your own thread, also we need to start to run from one function;
void my_print ()
{
    cout << "my thread my_print the Hello!" << endl;
    cout << "! My thread Hello my_print" << endl;
    cout << "! my thread my_print the Hello" << endl;
    cout << "! my thread my_print the Hello" << endl;
    cout << "! my thread my_print the Hello" << endl;
    cout << "I ! thread Hello my_print "<< endl;

    cout << "my thread execution completed ..." << endl;
}

main int ()
{
    / *
    main thread () is executed from main, then the thread of our own creation, but also need a function to start running (initial function) from, once the function has finished running, it represents the end of our run this thread.

    Mark whether the whole process is finished: if the main thread executed, if the main thread is finished, and on behalf of the entire process is finished;
    this time, under normal circumstances: if the other child thread has not been completed, the entire sub-thread the operating system will be forced to terminate.
    Therefore, under normal circumstances: the conclusion, if you want to keep the child thread (thread with its own code creates) is running, then you need to let the main thread to keep running, do not let it run to completion
    this rule there are exceptions, the teacher will explain the follow-up an exception to this we now understand and remember.
    a) contains a header file #include <Thread>
    B) initial write function.
    This is the need to be clear: there are two threads to run, equivalent to the implementation of the entire program has two lines go at the same time, we can do two things at once.
    Even if a line is blocked, it does not affect the execution of another line.
    (1.1) thread: is a standard class library
     std :: the Thread my_thread1 (my_print);
    A) is a callable object my_print
    b) creates a thread, the thread of execution entry function my_print
    c) my_print thread begins execution.

    (1.2) join (): join, convergence, it means blocking the main thread of the plunger, so that the child thread is finished, then the child thread and the main thread,
    so that the child thread and the main thread confluence, then the main thread then go down.
    my_thread1.join (); the main thread of the plunger here waiting my_print executed, the join () on the main thread is finished it continues downward.

    If the main thread is finished, but the child thread has not been executed, this program is failed, the program is also unstable.
    Writing a good program, it should be the main thread waits for the child thread execution is complete, quit their talent.
    (1.3) detach (): separation, which is the main thread does not and the child thread merging, you main thread to perform for you, my child thread execution me, you main thread also need not wait the end of my child thread running
    Why introduction detach () us created a lot of sub-thread, the main thread-by-thread waiting for the end of the child, this programming method is not good, so the introduction of the detach ();
    in fact, the best approach is to let the main thread wait for the end of each sub-thread execution.
    Once you detach, the number of days associated with the main thread object, it'll lose is associated with the main thread.
    Then the child will reside in the background thread, the main thread and the child thread lost contact with
    the child thread will be c ++ runtime library took over. When the child thread is finished, by the runtime library is responsible for cleaning up the thread-related resources (daemon thread)
    once called deatch (), you can not use the join (), otherwise the system reported abnormal.
    (1.4) joinable (): determines whether the join () or detach ().
     If the return true (may also be represented may join the detach)
     If returns false and represents not join the detach
    * /
    STD :: Thread my_thread1 (my_print,);
    COUT << my_thread1.joinable () << endl; // to true. 1
    //my_thread1.join();
    my_thread1.detach();
    cout <<my_thread1.joinable()<< endl;//0 false
    cout << "Hello world!" << endl;
    return 0;
}

 


    Two: to create a thread other way
    (2.1) with the class, as well as examples of a problem

#include <iostream>
#include <the Thread>
#include <String>
a using namespace std;
class A
{
public:
    void operator () () // can not have parameters
    {
        cout << "my thread operator to perform a" << endl ;
        cout << "my thread operator to perform a" << endl;
        cout << "my thread operator to perform a" << endl;
        cout << "my thread operator to perform a" << endl;
    }
};
int main ()
{
   
    A A1;
    STD :: thread my_thread2 ( A1 ); // Create a class object threads, the thread function is operator ()
    COUT << my_thread2.joinable () << endl;//true
    my_thread2.join();
    //my_thread1.detach();
    cout <<my_thread2.joinable()<< endl;//0 false
    cout << "Hello world!" << endl;
    return 0;
}

 

#include <iostream>
#include <thread>
#include <string>
using namespace std;

A class
{
public:
    int & m_i;
    A (I & int): m_i (I)
    {
        COUT << "constructor executes the" << endl;
    }
    A (const A & A1): m_i (a1.m_i)
    {
        COUT << "copy constructor executes" << endl;
    }
    ~ a ()
    {
        COUT << "destructor performs" << endl;
    }
    void operator () () // not with parameters
    {
        COUT << "operator my thread the implementation of the "<< endl;
        cout <<" m_i value:; "<< m_i << endl
        if the main thread is over, recovered m_i memory, it will // here m_i to the main thread, the problem, the invalid value printed
        cout << "m_i value:" << m_i << endl;
        cout << "m_i的值为:"<<m_i<< endl;
        cout << "m_i的值为:"<<m_i<< endl;
    }
};
() Int main
{
    / *
    two: create a thread other way
    (2.1) with the class, as well as examples of a problem

    */
    int my_i=6;

    A1 A (my_i);
    std :: the Thread my_thread2 (a1); // class object can be called using a1
    / * Once the call detach (), if the main thread is over, so here a1 objects still exist?
    In fact, this is a1 is copied to the thread went. So after executing the main thread a1 will be destroyed.
    But a1 copied to the target child thread still exists, so long as no object references and pointers a1 , then there is no problem
    * /
    //my_thread2.join ();
    my_thread2.detach ();
    cout << my_thread2.joinable () << endl; // 0 to false
    "! the Hello World" COUT << << endl;
    return 0;
}


Use my_thread2.detach (); the results seen from the results m_i <value a problem

Use my_thread2.join (); result,

Destructor first call is to copy the object into sub-thread is destructed

The second object destructor a1 is the main thread destructor

 

main int ()
{
    / *
    two: create a thread other way
 
    (2.2) using a lambda expression
    * /
   Auto my_lambda_th = []
    {
        // create a thread yourself, need to start to run from one function;
        cout << "My ! thread my_lambda_thread the Hello "<< endl;
        cout <<"! my thread my_lambda_thread the Hello "<< endl;
        cout <<"! my thread my_lambda_thread the Hello "<< endl;
        cout <<" my thread Hello my_lambda_thread "! endl <<;
        cout << "! my thread my_lambda_thread the Hello" endl <<;
        cout << "! my thread my_lambda_thread the Hello" << endl;
        cout << "my thread execution completed ..." << endl;
    };Semicolon here do not lose
    std :: thread my_thread3 (my_lambda_th); // lambda expression
    my_thread3.join ();
    //my_thread2.detach();
    cout << "Hello world!" << endl;
    return 0;
}

 

 

 

Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/104137971