Asynchronous Programming C ++ 11 std :: async and std :: future

C ++ 11 provides high-level abstraction of asynchronous tasks contained in the <future> header file, it allows us to easily achieve the implementation of a time-consuming task asynchronously, and get the result when needed. 
I cite a few examples: 
1. Batch copy / download files; 
2. a complicated calculation; 
3. perform multiple nested SQL queries; 
in actual development, computer perform these operations, you need a certain to return results after time, this time we need to use asynchronous. Compared directly compared using std :: thread, std :: async and std :: future has the following advantages: 
1.std :: Future async objects returned, you can easily wait for the completion of the implementation of callable objects and obtain its return value; 
2 . Some advanced features from the library to achieve benefit, such as thread pools, and greatly reduce the occurrence of an abnormality. 
We look at the actual code example Future :

#include <iostream>
#include <future>
#include <chrono>

bool is_prime(int x)
{
    for(int i=2;i<x;i++)
    {
        if(x%i==0)
            return false;

        return true;
    }
}

int main()
{
    std::future <bool> fut = std::async(is_prime,4444444444444444443);

    std::cout<<"wait,Checking";
    std::chrono::milliseconds span(10);
    while(fut.wait_for(span)==std::future_status::timeout)
        std::cout<<'.'<<std::flush;
        bool x = fut.get();
        std::cout<<"\n4444444444444444443"<<(x?" is":"is not") << " prime.\n";
        return 0;
}

 

std :: future can be used to obtain the results of asynchronous tasks, so you can use it as a simple means of inter-thread synchronization. std :: future typically created by a Provider, the Provider you can imagine a provider of asynchronous tasks, Provider settings in a shared state of thread, std :: future object associated with the shared state of calling get (usually in another thread) to get the value, if the shared state flag is not ready, then call the std :: future :: get will block the current caller until Provider sets the value of the shared state (shared state at this time the flag is ready), std :: future :: get return value or exception asynchronous tasks (if an exception occurs). 
The async function () can be used like thread, you can also start a thread, but the function returns the object is not a future thread object, so that the focus is not calculation process. Next, look at the code conversion between the thread and async Code:

#include <iostream>
#include <thread>
#include <future>

using namespace std;

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


    thread t([]{cout<<"hello"<<endl;});
    t.join();

    return 0;
}

The above code is equivalent to:

#include <iostream>
#include <thread>
#include <future>

using namespace std;


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

    auto f =async([]{cout<<"hello"<<endl;});
    f.wait();

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lx17746071609/p/11137198.html