C ++ parallel programming "a"

1. The simplest multithreaded C ++ program

#include <iostream>
#include <thread>

void do_something()
{
    std::cout << "func do_something..." << std::endl;
}

int main(int argc, char *argv[])
{

    std::thread my_thread(do_something);
    std::cout << "main thread" << std::endl;
    my_thread.join();
    return 0;
}

2. In the C ++ standard, std :: thread and can work with any type of call (Callable), therefore, we can give std :: thread constructor class instance with a transfer function call operator, instead of directly into the The function.

#include <iostream>
#include <thread>

void do_something()
{
    std::cout << "func do_something" << std::endl;
}

class background_task
{
public:
    void operator()() const
    {
        do_something();
    }
};

int main(int argc, char *argv[])
{
    background_task bt;
    std::thread my_thread(bt);      /* 传入一个类实例 */
    std::cout << "main thread" << std::endl;
    my_thread.join();

    return 0;
}

3. Wait for the end of the thread, C ++ there are ways (join, detach) the end of the two types of threads. For detach () method is concerned, even if the thread instance is destroyed the main thread, the thread can still perform background, until the end of their own.

As for the join () method, the main thread will wait for the child threads of execution, and get the return value of the sub thread. The question is, what are we waiting for child thread end where, if an exception occurs, how to ensure the child thread can still correct ending. A method as follows:

#include <iostream>
#include <thread>

void do_something()
{
    std::cout << "func do_something..." << std::endl;
}

class background_task
{
public:
    void operator()()
    {
        do_something();
    }
    background_task(int val)
    {
        std::cout << "background_task constructor init" << std::endl;
    }
};

int main(intargc, char * the argv []) 
{ 
    int the init_val = 0 ; 
    background_task BT (the init_val); 

    STD :: Thread my_thread (BT); 

    the try 
    { 
        STD :: COUT << " ERR occured here Wallpaper " << endl STD ::; / * error position * / 
    } 
    the catch (...) 
    { 
        my_thread.join ();                          / * in order to ensure the normal end of the thread can in exceptional circumstances * / 
        the throw ; 
    } 
    my_thread.join ();                              / * Program end position * / 
    return 0;
}

4. End Process try / catch mode, not only the code reader is reduced, and easy to confuse scope, therefore a need for a more brilliant manner. One way to do this is to use a resource acquisition is initialization (RALL) idioms.

as follows:

#include <the iostream> 
#include <Thread> class thread_guard 
{ 
    STD :: Thread & T;            / * Define an alias for the incoming thread copied to it * / public :
     Explicit thread_guard (STD :: Thread & T_): T ( T_) {}
     ~ thread_guard () 
    { / * the end of the process operation and the class resource release binding, as long as the class has been released, the thread will end * / IF (t.joinable ()) 
        { 
            t.join (); 
        } 
    } 
    thread_guard (thread_guard const &) = Delete ; 
    thread_guard & operator




        = (thread_guard const &) = Delete ; 
}; 

void do_something () 
{ 
    STD :: COUT << " FUNC do_something " << STD :: endl; 
} 

struct FUNC 
{ 
    int & I; 
    FUNC ( int & I_): I ( I_) {}
     void  operator () () 
    { 
        do_something (); 
    } 
}; 

/ * method a simple, it is possible to ensure that under abnormal conditions can be normal end thread * / 
int main ( int argc, char * the argv [ ])
{
    int the init_val = 0 ; 
    FUNC my_func (the init_val); 
    STD :: Thread my_thread (my_func); 
    thread_guard G (my_thread); 
    return  0 ;                           / * After the end of the main thread, will destroy the local variable is destroyed g, it waits for the child end of the thread * / 
}

 

Guess you like

Origin www.cnblogs.com/PPWEI/p/11422424.html