c ++ 11 multi-threaded programming (Part 3)

 

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

 // create your own thread, also need to start to run from one function;
// void my_print (const int i, char * p_myBuf)
void my_print (const int i, const String & p_myBuf)
{
    cout << "i:" << i endl <<;
    // analysts believe, i m1 is not a reference to the actual value is passed, even if the main thread end, nor is the use of sub-thread of the impact of this variable
    cout << "p_myBuf:" << p_myBuf << endl ;
    // pointer when deatch child thread, definitely have problems, so here must not pass a pointer
    return;
}
int main ()
{
    / *
   a: zero object as a thread passed parameters
    (2.1) as long as the temporary configuration a class object passed as a parameter to the thread, then it must be able to construct the thread function parameters before the main thread is finished out
    if this type of simple parameter passing int, recommendations are passed by value, do not use reference variable is the end of the simulation the main thread release cause problems.
    If a class object passed, to avoid implicit conversion. All in this line will create a thread to construct temporary objects, and then use the function parameters is referenced to pick.
    If you do not use references, and so will be more than one call copy constructor.
    (2.2)
    II: temporary object as a parameter thread
    The concept of thread id: id is a number, each thread (either the main thread or sub-thread) actually corresponds to a number.
    Different thread id must be different.
    Thread id can use c ++ standard library function to get std: this_thread :: get_id ();
    * /
    int M1 = 1;
    int & M2 = M1;
    char mybuf [] = "A the this IS the Test!";
    // actually mybuf was recovered (main finished execution) only with the system turned into mybuf string, so that there will be problems
    // std :: thread my_thread4 (my_print, m1, mybuf);
    converted into a zero mybuf objects, so that we can ensure that no problems.
    my_thread4 the Thread :: std (my_print, M1, String (mybuf));
    //my_thread4.join ();
    my_thread4.detach (); // child thread and main thread are executed
    "! Hello world" cout << << endl ;
    return 0;
}

-----------------------------------------------------------------------------------------------------------------------------

 

 

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

class A
{
public:
    int m_i;
    A(int i):m_i(i)
    {
        cout << "构造函数执行了"<<this<<" thread ID:"<<std::this_thread::get_id()<< endl;
    }
    A(const A &a1):m_i(a1.m_i)
    {
        cout << "拷贝构造函数执行" <<this<<" thread ID:"<<std::this_thread::get_id()<< endl;
    }
    ~A()
    {
        cout << "析构函数执行" <<this<<" thread ID:"<<std::this_thread::get_id()<< endl;
    }
    void operator()()//不能带参数
    {
        cout << "我的线程 operator执行了" << endl;
        cout << "m_i的值为:"<<m_i<< endl;

    }
};

void myprint2(const A &pmybuf)
{
    cout<<"子线程 myprint2参数地址"<<&pmybuf
        <<"threadid:"<<std::this_thread::get_id()<<endl;
}

main int ()
{
    / *
    (2.2)
    II: thread as a temporary object parameter
    concept of thread id: id is a number, each thread (whether the main thread or child threads) actually corresponds to a number.
    Different thread id must be different.
    Thread id can use c ++ standard library function to get the std :: this_thread :: get_id ();

    * /
    COUT << "main thread ID:" << this_thread :: get_id () << endl;
    int = M1. 1;
    // temporary objects used to pass the main thread, the thread will be safe.
    // class object when it is passed using references connected when the main thread creates a thread receive temporary objects , which can be thread-safe and less time calling the copy constructor.
    Thread my_thread5 :: STD (myprint2, A (M1) );

    mybuf char [] = "the this IS A Test!";
    //my_thread5.join ();
    my_thread5.detach (); // child thread and main thread are executed
    COUT << "! the Hello World" << endl;
    return 0 ;
}

 

Smart pointer as a parameter to a function of the thread
void my_print (unique_ptr <int> pIn ) // thread function
{
    COUT << "my_print," <<<< "the threadId:" << this_thread :: :: get_id STD () << endl;
    
}

int main()
{
    unique_ptr<int> pInt(new int(100));
    std::thread myjob_th(my_print,std::move(pInt));
    myjob_th.join();
    //myjob_th.detach();
    
    
    return 0;
}

 

 

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

Guess you like

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