欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++11之std::async 和std::thread的區(qū)別小結(jié)

 更新時(shí)間:2024年02月20日 14:44:59   作者:奧特神龜1.1  
std::async和std::thread都是C++11中提供的線程庫(kù),它們都可以用于創(chuàng)建新線程,本文主要介紹了c++11之std::async 和std::thread的區(qū)別小結(jié),感興趣的可以了解一下

std::async和std::thread都是C++11中提供的線程庫(kù),它們都可以用于創(chuàng)建新線程。它們的主要區(qū)別在于:

  • std::async有時(shí)候并不創(chuàng)建新線程,而是使用線程池中的線程來執(zhí)行任務(wù),這取決于實(shí)現(xiàn)。而std::thread總是創(chuàng)建新線程。
  • std::async返回一個(gè)std::future對(duì)象,可以用來獲取異步任務(wù)的結(jié)果。而std::thread沒有返回值,需要通過其他方式來獲取線程執(zhí)行的結(jié)果。
  • std::async可以指定任務(wù)的執(zhí)行策略,例如std::launch::async表示一定要?jiǎng)?chuàng)建新線程執(zhí)行任務(wù),std::launch::deferred表示可以不創(chuàng)建新線程,等到調(diào)用std::future的get()方法時(shí)再執(zhí)行任務(wù)。而std::thread沒有這樣的選項(xiàng)。
#include <iostream>
#include <future>
#include <thread>

int task()
{
    std::cout << "Task is running in thread " << std::this_thread::get_id() << std::endl;
    return 42;
}

int main()
{
    // 使用std::async創(chuàng)建異步任務(wù)
    std::future<int> f1 = std::async(std::launch::async, task);
    std::cout << "Async task has been started." << std::endl;

    // 使用std::thread創(chuàng)建新線程
    std::thread t(task);
    std::cout << "Thread has been started." << std::endl;

    // 等待異步任務(wù)完成并獲取結(jié)果
    int result1 = f1.get();
    std::cout << "Async task has been finished with result " << result1 << std::endl;

    // 等待線程完成并退出
    t.join();
    std::cout << "Thread has been finished." << std::endl;

    return 0;
}

更直觀的看一下std::launch::async 與 ,std::launch::deferred

#include <iostream>
#include <future>

int main() {
    auto f1 = std::async(std::launch::deferred, []() {
        std::cout << "This is a deferred async task." << std::endl;
        return 1;
        });
    auto f2 = std::async(std::launch::async, []() {
        std::cout << "This is a async task." << std::endl;
        return 2;
        });
    std::cout << "Waiting..." << std::endl;
    std::cout << f1.get() << std::endl;
    std::cout << f2.get() << std::endl;
    return 0;
}

std::async不確定問題的解決

不加額外參數(shù)的std::async調(diào)用問題,讓系統(tǒng)自行決定是否創(chuàng)建新的線程。

問題的焦點(diǎn)在于 std::future<int> result = std::async(mythread)寫法,這個(gè)異步任務(wù)到底 有沒有被推遲執(zhí)行。

實(shí)例代碼如下:

#include<iostream>
#include<thread>
#include<string>
#include<vector>
#include<list>
#include<mutex>
#include<future>
using namespace std;
int mythread() //線程入口函數(shù)
{
    cout << "mythread start" << "threadid= " << std::this_thread::get_id() << endl; //打印線程id
    std::chrono::milliseconds dura(5000); //定一個(gè)5秒的時(shí)間
    std::this_thread::sleep_for(dura);  //休息一定時(shí)常
    cout << "mythread end" << "threadid= " << std::this_thread::get_id() << endl; //打印線程id
    return 5;
}
int main()
{
    cout << "main" << "threadid= " << std::this_thread::get_id() << endl;
    std::future<int> result = std::async(mythread);//流程并不卡在這里
    cout << "continue....." << endl;
    //枚舉類型
    std::future_status status = result.wait_for(std::chrono::seconds(0));//等待一秒
    
    if (status == std::future_status::deferred)
    {
        //線程被延遲執(zhí)行了,系統(tǒng)資源緊張
        cout << result.get() << endl; //此時(shí)采取調(diào)用mythread()
    }
    else if (status == std::future_status::timeout)//
    {
        //超時(shí):表示線程還沒執(zhí)行完;我想等待你1秒,希望你返回,你沒有返回,那么 status = timeout
        //線程還沒執(zhí)行完
        cout << "超時(shí):表示線程還沒執(zhí)行完!" << endl;
    }
    else if (status == std::future_status::ready)
    {
        //表示線程成功返回
        cout << "線程成功執(zhí)行完畢,返回!" << endl;
        cout << result.get() << endl;
    }
    cout << "I love China!" << endl;
    return 0;
}

到此這篇關(guān)于c++11之std::async 和std::thread的區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)c++11 std::async 和std::thread內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論