亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++中的std::funture和std::promise實(shí)例詳解

 更新時(shí)間:2024年05月20日 11:57:58   作者:vegetablesssss  
在線程池中獲取線程執(zhí)行函數(shù)的返回值時(shí),通常使用 std::future 而不是 std::promise 來傳遞返回值,這篇文章主要介紹了C++中的std::funture和std::promise實(shí)例詳解,需要的朋友可以參考下

std::funture和std::promise

#include <iostream>
#include <thread>
#include <future>
void calculateResult(std::promise<int>& promiseObj) {
	// 模擬耗時(shí)計(jì)算
	std::this_thread::sleep_for(std::chrono::seconds(2));
	// 設(shè)置結(jié)果到 promise 中
	promiseObj.set_value(42);
}
int main() {
	// 創(chuàng)建一個(gè) promise 對(duì)象
	std::promise<int> promiseObj;
	// 獲取與 promise 關(guān)聯(lián)的 future
	std::future<int> futureObj = promiseObj.get_future();
	// 啟動(dòng)一個(gè)新的線程執(zhí)行計(jì)算
	std::thread workerThread(calculateResult, std::ref(promiseObj));
	// 在主線程中等待任務(wù)完成,并獲取結(jié)果
	int result = futureObj.get();
	std::cout << "Result: " << result << std::endl;
	// 等待工作線程結(jié)束
	workerThread.join();
	return 0;
}

使用future和promise可以獲取到線程執(zhí)行函數(shù)的結(jié)果,類似C#實(shí)現(xiàn)

#include <iostream>
#include <thread>
#include <future>
#include <vector>
#include <functional>
#include <queue>
class ThreadPool {
public:
    explicit ThreadPool(size_t numThreads) : stop(false) {
        for (size_t i = 0; i < numThreads; ++i) {
            threads.emplace_back([this] {
                while (true) {
                    std::function<void()> task;
                    {
                        std::unique_lock<std::mutex> lock(mutex);
                        condition.wait(lock, [this] { return stop || !tasks.empty(); });
                        if (stop && tasks.empty()) {
                            return;
                        }
                        task = std::move(tasks.front());
                        tasks.pop();
                    }
                    task();
                }
                });
        }
    }
    ~ThreadPool() {
        {
            std::unique_lock<std::mutex> lock(mutex);
            stop = true;
        }
        condition.notify_all();
        for (std::thread& thread : threads) {
            thread.join();
        }
    }
    template <class F, class... Args>
    auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> {
        using return_type = typename std::result_of<F(Args...)>::type;
        auto task = std::make_shared<std::packaged_task<return_type()>>(
            std::bind(std::forward<F>(f), std::forward<Args>(args)...)
        );
        std::future<return_type> result = task->get_future();
        {
            std::unique_lock<std::mutex> lock(mutex);
            if (stop) {
                throw std::runtime_error("enqueue on stopped ThreadPool");
            }
            tasks.emplace([task]() { (*task)(); });
        }
        condition.notify_one();
        return result;
    }
private:
    std::vector<std::thread> threads;
    std::queue<std::function<void()>> tasks;
    std::mutex mutex;
    std::condition_variable condition;
    bool stop;
};
int calculateResult() {
    // 模擬耗時(shí)計(jì)算
    std::this_thread::sleep_for(std::chrono::seconds(2));
    return 42;
}
int main() {
    ThreadPool pool(4);
    std::future<int> futureObj = pool.enqueue(calculateResult);
    int result = futureObj.get();
    std::cout << "Result: " << result << std::endl;
    return 0;
}

在線程池中獲取線程執(zhí)行函數(shù)的返回值時(shí),通常使用 std::future 而不是 std::promise 來傳遞返回值。這是因?yàn)榫€程池內(nèi)部已經(jīng)管理了任務(wù)的執(zhí)行和結(jié)果的傳遞,你只需要將任務(wù)提交給線程池,并使用 std::future 來獲取結(jié)果。

線程池內(nèi)部一般會(huì)使用一個(gè)任務(wù)隊(duì)列來存儲(chǔ)待執(zhí)行的任務(wù),并使用一個(gè)線程池管理器來調(diào)度任務(wù)的執(zhí)行。當(dāng)你向線程池提交任務(wù)時(shí),線程池會(huì)選擇一個(gè)空閑的線程來執(zhí)行任務(wù),并將結(jié)果存儲(chǔ)在與任務(wù)關(guān)聯(lián)的 std::future 對(duì)象中。

擴(kuò)展:C# 異步

首先說幾個(gè)關(guān)鍵詞:

async:修飾方法,表明該方法是異步的
Task:異步任務(wù),可以作為異步方法的返回值
await:等待異步方法執(zhí)行完成,只能在異步方法中使用
TaskCompletionSource:可通過SetResult方法來設(shè)置異步的結(jié)果

using System;
using System.Threading.Tasks;
class Program
{
    static async void AsyncMethod()
    {
        await Task.Run(() =>
        {
            Console.WriteLine("耗時(shí)操作");
        });
        await AsyncTaskCompletion();
    }
    static async Task<int> AsyncTaskCompletion()
    {
        Console.WriteLine("AsyncTaskCompletion Start");
        TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
        // 模擬異步操作
        await Task.Delay(3000).ContinueWith(task =>
        {
            if (task.Exception != null)
            {
                tcs.SetException(task.Exception);
            }
            else
            {
                // 設(shè)置異步操作的結(jié)果
                tcs.SetResult(42);
            }
        });
        Console.WriteLine("AsyncTaskCompletion END");
        return await tcs.Task;
    }
    static void Main()
    {
        Console.WriteLine("Main Start");
        AsyncMethod(); 
        Console.WriteLine("Main END");
        Console.ReadLine();
    }
}

main方法中執(zhí)行一個(gè)返回值為空的異步方法AsyncMethod,在AsyncMethod可以執(zhí)行各種耗時(shí)操作而不影響main方法執(zhí)行。
在AsyncTaskCompletion方法中等待tcs.SetResult方法執(zhí)行后才會(huì)返回,可用于在回調(diào)函數(shù)中設(shè)置結(jié)果。

到此這篇關(guān)于C++中的std::funture和std::promise實(shí)例詳解的文章就介紹到這了,更多相關(guān)std::funture和std::promise內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用c語(yǔ)言判斷100以內(nèi)素?cái)?shù)的示例(c語(yǔ)言求素?cái)?shù))

    使用c語(yǔ)言判斷100以內(nèi)素?cái)?shù)的示例(c語(yǔ)言求素?cái)?shù))

    這篇文章主要介紹了使用c語(yǔ)言判斷100以內(nèi)素?cái)?shù)的示例(c語(yǔ)言求素?cái)?shù)),需要的朋友可以參考下
    2014-03-03
  • c++類和對(duì)象基本概念

    c++類和對(duì)象基本概念

    這篇文章主要為大家介紹了c++類和對(duì)象,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • C語(yǔ)言超細(xì)致講解函數(shù)遞歸

    C語(yǔ)言超細(xì)致講解函數(shù)遞歸

    程序調(diào)???的編程技巧稱為遞歸?recursion)函數(shù)??調(diào)???就是遞歸,你也可以理解成是?種嵌套結(jié)構(gòu),但遞歸分為倆部分,第?是“遞”,進(jìn)?嵌套結(jié)構(gòu)。第?是”歸“,最終會(huì)?步?步返回。第?次接觸遞歸都會(huì)很懵,慢慢理解這個(gè)過程就明?了
    2022-05-05
  • 雙緩沖解決VC++繪圖時(shí)屏幕閃爍

    雙緩沖解決VC++繪圖時(shí)屏幕閃爍

    相信很多人在做圖形界面開發(fā)時(shí),常常會(huì)遇到屏幕閃爍的情況,當(dāng)然我也不例外,下面我們就來詳細(xì)探討下這個(gè)問題的解決辦法
    2015-08-08
  • MFC控件之CListCtrl的應(yīng)用實(shí)例教程

    MFC控件之CListCtrl的應(yīng)用實(shí)例教程

    這篇文章主要介紹了MFC控件中CListCtrl的應(yīng)用方法,包括了針對(duì)表格的一些操作,是MFC中比較重要的一個(gè)控件類,需要的朋友可以參考下
    2014-08-08
  • Cocos2d-x中背景音樂和音效使用實(shí)例

    Cocos2d-x中背景音樂和音效使用實(shí)例

    這篇文章主要介紹了Cocos2d-x中背景音樂和音效使用實(shí)例,注意本文中使用大量注釋來說明背景音樂和音效的使用方法,需要的朋友可以參考下
    2014-09-09
  • C語(yǔ)言實(shí)現(xiàn)消消樂游戲

    C語(yǔ)言實(shí)現(xiàn)消消樂游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)消消樂游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • c語(yǔ)言中scanf的基本用法

    c語(yǔ)言中scanf的基本用法

    這篇文章主要給大家介紹了關(guān)于c語(yǔ)言中scanf的基本用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows)

    Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows)

    Clion2020增加了很多新特性,修復(fù)了大量bug,大大提高了開發(fā)效率。這篇文章主要介紹了Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows),需要的朋友可以參考下
    2020-11-11

最新評(píng)論