Eleven, atomic, async depth

One, atomic operation

g++;

g+=1;

g = g + 1; // do not result

General atomic operations for ++, -, + =, & =, | =, ^ = support, the other may not support

Two, std :: async depth

To create asynchronous tasks.

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 
 5 int mythread(){
 6     cout<<"mythread is begining"<<endl;
 7     return 1;
 8 }
 9 
10 int main(){
11     std::future<int> result = std::async(mythread);//默认std::launch::async | std::launch::defered
12     cout<<result.get()<<endl;
13 }

 

1, async parameter

Delayed call: std :: launch :: defered

. 1 #include <the iostream>
 2 #include <Thread>
 . 3  the using  namespace STD;
 . 4  
. 5  int MyThread () {
 . 6      COUT << " MyThread IS Begining " << endl;
 . 7      return  . 1 ;
 . 8  }
 . 9  
10  int main () {
 11      std :: Future < int > the Result = std :: (std :: :: Launch defered, MyThread) the async; // delay call, do not create a new thread, only calls get / wait function will execute threads entrance function 
12 is      . COUT << Result GET () << endl;
 13 is }

 

Force the creation of a std :: launch :: async thread

#include <the iostream> 
#include <Thread>
 the using  namespace STD; 

int MyThread () { 
    COUT << " MyThread IS Begining " << endl;
     return  . 1 ; 
} 

int main () { 
    STD :: Future < int > Result = STD the async :: (std :: :: Launch the async, MyThread); // force the asynchronous task execution on a new thread, it means that the system must create a new thread to run the thread entry function 
    . cout << the Result GET () << endl; 
}

 

std::launch::async | std::launch::defered

. 1 #include <the iostream>
 2 #include <Thread>
 . 3  the using  namespace STD;
 . 4  
. 5  int MyThread () {
 . 6      COUT << " MyThread IS Begining " << endl;
 . 7      return  . 1 ;
 . 8  }
 . 9  
10  int main () {
 11      // | mean, call async behavior may create a new thread, there may not be created, the system selects its own 
12      std :: Future < int > the Result = std :: async (std :: :: Launch async | std :: :: defered Launch, MyThread);
 13      . cout << the Result GET () <<endl;
14 }

 

No arguments with std :: launch :: async | as std :: launch :: defered, the system will be to decide asynchronous (to create a new thread) or synchronous (does not create a new thread) run.

2, async and thread difference

thread is designed to create a thread, if the system resource constraints, thread creates a thread may fail, the entire program may crash when performing therad; and async calls without additional parameters will not create a new thread, but the follow-up who get called function to request, before the asynchronous task mythread runs on the thread where this get statements (such as get in the main, then the equivalent of calling mythread function in the main thread)

async generally do not create a thread called and told to create an asynchronous task.

The most obvious difference: async sometimes does not create a thread, for example, the code above, only when the function is called, will get to create a thread, otherwise it will not be executed.

If you want to take a thread return value, you must also set a global variable assignment. async easy to get the thread function's return value

 

The number of threads can not exceed 100 to 200, there is a time slice,

3, async uncertainty

There is no judgment async creates a thread?

Use wait_for ()

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

int mythread(){
    cout<<"mythread is begining"<<endl;
    return 1;
}

int main(){
    std::future<int> result = std::async(mythread);
    //result.wait_for(10s),(10min)都可以
    std::future_status status=result.wait_for(std::chrono::seconds(0));//0ms
    if(status==std::future_status::deferred){
        //Do not create a thread immediately, delaying the implementation of the 
        cout << the Result. GET () << endl; 
    } 
    the else {
         // thread without delay 
        
        // thread runs over 
        IF (Status == std :: :: future_status READY) {
             // thread run over, successful return 
        }
         the else  IF (Status == std :: :: future_status timeout) {
         // timeout, the thread has not been performed, and other threads executing the SLEEP 
        
        
        } 
    } 
    
    
}

 

Guess you like

Origin www.cnblogs.com/pacino12134/p/11270846.html