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)文章希望大家以后多多支持腳本之家!
- C++中std::setw()的用法解讀
- c++中std::placeholders的使用方法
- c++ std::sort使用自定義的比較函數(shù)排序方式
- C++中std::thread{}和std::thread()用法
- C++中std::tuple和std::pair的高級(jí)用法
- c++之std::get_time和std::put_time
- C++中std::ios_base::floatfield報(bào)錯(cuò)已解決
- C++中std::invalid_argument報(bào)錯(cuò)解決
- C++中std::ifstream::readsome和std::ifstream::read的區(qū)別解析
- C++中std::transform的使用小結(jié)
- C++?std::copy與memcpy區(qū)別小結(jié)
- C++實(shí)現(xiàn)std::set的示例項(xiàng)目
相關(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ù)),需要的朋友可以參考下2014-03-03MFC控件之CListCtrl的應(yīng)用實(shí)例教程
這篇文章主要介紹了MFC控件中CListCtrl的應(yīng)用方法,包括了針對(duì)表格的一些操作,是MFC中比較重要的一個(gè)控件類,需要的朋友可以參考下2014-08-08C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的三子棋游戲源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows)
Clion2020增加了很多新特性,修復(fù)了大量bug,大大提高了開發(fā)效率。這篇文章主要介紹了Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows),需要的朋友可以參考下2020-11-11