(12) Multi-threading advanced features of Boost library

Source: Internet
Author: User

(12) Multi-threading advanced features of Boost library

Most of the time, the thread is not just doing some time-consuming operations, maybe we also need to get the thread return value, the general way is to define a global state variable, constantly rotation state, as I am currently maintaining a project, the global variable defines n state, see the person is crazy. The general logic of the project is to start a K-thread, when the thread executes to a point, to rotation, to determine if all threads are executing to that point, to open a thread for rotation all threads to end, to fetch data after all threads are finished, to generate a file, There is also a thread on whether the rotation file is generated and then reads the file for next steps. All sorts of rotation, seem very clumsy. Using the Boost library, let's take a look at how to solve these synchronization problems.

1. Get thread Results

Boost::p Ackaged_task wraps a callable object and allows the result to be obtained asynchronously from the callable object

Unique_future for saving results from asynchronous computations

voidgetfutures() 
{
    Boost::p ackaged_task<));
    boost::unique_future<int> uf = pt.get_future ();
    Boot thread, must use Move,packaged_task is non-copy
    Boost::th(Boost::move (PT)); 
    Uf.wait ();
    int nval = Uf.get ();
    "<< nval << Endl;
}

Uf.wait waits for the thread to end, and of course the future class also offers many kinds of waiting functions, such as timed_wait waiting for a while.

Of course, we are more often waiting for the end of a group of threads, which can be used Wait_for_all to wait for all future objects, Wait_for_any wait for any object to be received.

Getfutures ()
{
    Boost::p ackaged_task<int> pt1 (boost:: Bind);     
    Boost::p ackaged_task<int> pt2 (boost:: Bind);     
    Boost::unique_future<int> UF1 = pt1.get_future ();  
    Boost::unique_future<int> UF2 = pt2.get_future ();  
    Boost:: Thread (boost:: Move (PT1));  
    Boost:: Thread (boost:: Move (PT2));  
    Boost:: Wait_for_all (UF1, uf2);
    "" ,"<< uf2.get () << Endl;  
}
2, Guardrail barrier

The fence means waiting for all threads to reach the same point before proceeding.

Boost::BR(3); 
voidBarrierfunc() 
{
    "begin" << Endl; 
    Br.wait ();
    "<< Endl;
}
voidtestbarrier() 
{
    Boost::thread_group GRP;
    Grp.create_thread (Barrierfunc);
    Grp.create_thread (Barrierfunc);
    Grp.create_thread (Barrierfunc);
    Grp.join_all ();
}

3. Thread Local Storage

The use of global variables or local static variables, which is very common, but such a function for multi-threaded programs, it is difficult to ensure that the correctness of the program, we hope that these global variables and local static variables are independent of the thread, the multiple threads will not cause interference, then use Thread_ Specific_ptr can be easily solved.

intAdd(int n)   
{
    Static boost::thread_specific_ptr<int> sp;  //The variable is owned by the thread independently 
    if (!sp.get ())
    {
        Sp.reset (Int (0)); 
    }
    *SP = n + *sp;
    return *SP;
}
void  Sum() 
{
    cout <<  Add (5) + Add (ten) << Endl;           //Results obtained  
}
voidthreadsum() 
{
    Boost::thread_group threads;    
    for (int i=0; i<5; ++i)  
        Threads.create_thread (&sum);             //The result of all threads output is the same
    Threads.join_all ();
}

(12) Multi-threading advanced features of Boost library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.