C++多线程编程第十一讲--std::atomic续谈、std::async深入谈
//(1)原子操作std::atomic续谈
// 原子操作针对++,--,+=,-=,&=,|=,~=是支持的,其他的可能不支持。
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
using namespace std;
//int g_mycount = 0;
std::atomic<int> g_mycount = 0;   //具备原子操作的整型值
void my_thread()
{
    int i = 0;
    for (; i < 1000000; ++i)
    {
        g_mycount++;
        //g_mycount += 1;  正确
        //g_mycount = g_mycount + 1;   //结果错误
    }
}
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
    thread mythread(my_thread);
    thread mythread2(my_thread);
    mythread.join();
    mythread2.join();
    cout << g_mycount << endl;
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
//(2)std::async深入谈
// std::async和thread最大的不同就是std::async可能不创建新的线程,具体的执行动作可以由参数决定。
	//(2.1) std::async参数详解
    // std::launch::deferred  延迟调用,不会创建一个新的线程
    // std::launch::async    立即创建一个线程
    // 如果什么也不写,默认值是std::launch::async | std::launch::deferred
    // 系统会根据实际情况选择其中之一来执行。系统资源紧缺时,会使用std::launch::deferred,因为不需要创建新的线程;
    // 如果系统资源宽松时,就会选择std::launch::async,创建一个新的线程来执行代码。
	//(2.2) std::async和std::thread的区别
    // 在资源紧张时,使用thread来创建线程,如果发生错误,程序会直接崩溃。
    // 这个时候如果使用std::async就可能不创建新的线程。
    // std::async也很容易拿到线程的返回值。
#include<iostream>
    #include<mutex>
    #include<thread>
    #include<future>
    
    using namespace std;
    
    
    int my_thread()
    {
        cout << "my_thread start... " << "thread id = " << std::this_thread::get_id() << endl;
        std::chrono::milliseconds dura(3000);
        std::this_thread::sleep_for(dura);
        cout << "my_thread end... " << "thread id = " << std::this_thread::get_id() << endl;
    
        return 5;
    }
    
    
    int main()
    {
        cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
    
        std::future<int> result = std::async(my_thread);
    
        cout << result.get() << endl;
    
        cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
        return 0;
    }
//(2.3) std::async不确定性问题的解决
    //可以用std::future的wait_for方法来判断线程到底有没有创建新的线程
#include<iostream>
#include<mutex>
#include<thread>
#include<future>
using namespace std;
int my_thread()
{
    cout << "my_thread start... " << "thread id = " << std::this_thread::get_id() << endl;
    std::chrono::milliseconds dura(3000);
    std::this_thread::sleep_for(dura);
    cout << "my_thread end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 5;
}
int main()
{
    cout << "main() start... " << "thread id = " << std::this_thread::get_id() << endl;
    std::future<int> result = std::async(my_thread);
    // std::future_status status = result.wait_for(0s);
    std::future_status status = result.wait_for(std::chrono::seconds(0));   //等待0秒
     if(status == std::future_status::deferred)
    {
        cout << "no thread created..." << endl;
    }
     else
     {
         cout << "new thread start..." << endl;
     }
    cout << result.get() << endl;
    cout << "main() end... " << "thread id = " << std::this_thread::get_id() << endl;
    return 0;
}
posted on 2021-10-27 21:40 xcxfury001 阅读(45) 评论(0) 收藏 举报
 
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号