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

深入了解C++11中promise和future的使用

 更新時間:2022年11月11日 11:31:40   作者:小小碼農(nóng)Come?on  
C++11中promise和future機制是用于并發(fā)編程的一種解決方案,用于在不同線程完成數(shù)據(jù)傳遞(異步操作)。Promise和Future提供了訪問異步操作結(jié)果的機制,可以在線程之間傳遞數(shù)據(jù)和異常消息。本文就來聊聊二者的使用,希望對大家有所幫助

Promise和Future

原理

C++11中promise和future機制是用于并發(fā)編程的一種解決方案,用于在不同線程完成數(shù)據(jù)傳遞(異步操作)。

傳統(tǒng)方式通過回調(diào)函數(shù)處理異步返回的結(jié)果,導(dǎo)致代碼邏輯分散且難以維護。

Promise和Future是一種提供訪問異步操作結(jié)果的機制,可以在線程之間傳遞數(shù)據(jù)和異常消息。

應(yīng)用場景:顧客在一家奶茶店點了單,服務(wù)員給顧客一個單號,當奶茶做好后,服務(wù)員更新排號的狀態(tài),顧客可以去做自己的事情了,顧客可以通過查詢排號來得知奶茶是否做好,當查到奶茶做好了就可以回來取奶茶了。

#include <iostream>
#include <future>
#include <thread>

using namespace std;
using namespace std::chrono;

void WaitForMilkTea(future<int>& future)
{
	/*其中獲取future結(jié)果有三種方式
	 1、auto value = future.get()    get()方法會阻塞等待異步操作結(jié)束并返回結(jié)果
	 2、std::future_status  方式判斷狀態(tài) 有deferred、timeout、ready三種狀態(tài)
	 3、可以
	*/

//future_status方法
#if 0
	std::future_status status;
	do {
		status = future.wait_for(std::chrono::milliseconds(500));
		if (status == std::future_status::deferred) {
			std::cout << "deferred!!!" << std::endl; //異步操作還沒開始
		} else if (status == std::future_status::timeout) {
			std::cout << "timeout!!!" << std::endl; //異步操作超時
		} else if (status == std::future_status::ready) {
			std::cout << "ready!!!" << std::endl; //異步操作已經(jīng)完成
		}
	} while (status != std::future_status::ready);
     
	//通過判斷future_status狀態(tài)為ready時才通過get()獲取值
	auto notice = future.get();
	std::cout << "WaitForMilkTea receive value:" << notice << std::endl;
#endif

//get()方法
#if 0
	auto notice = future.get();   //get阻塞等待直到異步處理結(jié)束返回值
	std::cout << "WaitForMilkTea receive value:" << notice << std::endl;
#endif

//wait()方法
	future.wait(); //和get()區(qū)別是wait只等待異步操作完成,沒有返回值
	auto notice = future.get();
	std::cout << "WaitForMilkTea receive value:" << notice << std::endl;
}

void MakeTea(promise<int>& promise)
{
	//do something 這里先睡眠5s
	std::this_thread::sleep_for(std::chrono::seconds(5));
	promise.set_value(1);
	std::this_thread::sleep_for(std::chrono::seconds(10));
	std::cout << "MakeTea Thread quit!!!" << std::endl;
}

int main()
{
	promise<int> pNotice;
	//獲取與promise相關(guān)聯(lián)的future
	future<int> pFuture = pNotice.get_future();

	thread Customer(WaitForMilkTea, ref(pFuture));
	thread Worker(MakeTea, ref(pNotice));

	Customer.join();
	Worker.join();
}

其中future_status枚舉如下:

名稱示意
ready0就緒
timeout1等待超時
deferred2延遲執(zhí)行(與std::async配合使用)

future_errc 枚舉 : 為 future_error 類報告的所有錯誤提供符號名稱。

名稱示意
broken_promise0與其關(guān)聯(lián)的 std::promise 生命周期提前結(jié)束
future_already_retrieved1重復(fù)調(diào)用 get() 函數(shù)
promise_already_satisfied2與其關(guān)聯(lián)的 std::promise 重復(fù) set
no_state4無共享狀態(tài)

Promise和Future模型

流程如下:

1.線程1初始化一個promise和future對象,將promise對象傳遞給線程2,相當于線程2對線程1的一個承諾

2.future相當于一個承諾,用于獲取未來線程2的值

3.線程2接受一個promise,需要將處理結(jié)果通過promise返回給線程1

4.線程1想要獲取數(shù)據(jù),此時線程2還未返回promise就阻塞等待處,直到線程2的數(shù)據(jù)可達

promise相關(guān)函數(shù)

std::future負責訪問, std::future是一個模板類,它提供了可供訪問異步執(zhí)行結(jié)果的一種方式。

std::promise負責存儲, std::promise也是一個模板類,它提供了存儲異步執(zhí)行結(jié)果的值和異常的一種方式。

總結(jié):std::future負責訪問,std::promise負責存儲,同時promise是future的管理者

std::future

名稱作用
operator=移動 future 對象,移動!
share()返回一個可在多個線程中共享的 std::shared_future 對象
get()獲取值(類型由模板類型決定)
valid()檢查 future 是否處于被使用狀態(tài),也就是它被首次在首次調(diào)用 get() 或 share() 前。建議使用前加上valid()判斷
wait()阻塞等待調(diào)用它的線程到共享值成功返回
wait_for()在規(guī)定時間內(nèi) 阻塞等待調(diào)用它的線程到共享值成功返回
wait_until()在指定時間節(jié)點內(nèi) 阻塞等待調(diào)用它的線程到共享值成功返回

1、普通構(gòu)造函數(shù), 默認無參構(gòu)造函數(shù)

2、帶自定義內(nèi)存分配器的構(gòu)造函數(shù),與默認構(gòu)造函數(shù)類似,但是使用自定義分配器來分配共享狀態(tài)。

3、拷貝構(gòu)造函數(shù)和普通=賦值運算符默認禁止

4、移動構(gòu)造函數(shù)

5、移動賦值運算符

  • std::future僅在創(chuàng)建它的std::promise(或者std::async、std::packaged_task)有效時才有用,所以可以在使用前用valid()判斷
  • std::future可供異步操作創(chuàng)建者用各種方式查詢、等待、提取需要共享的值,也可以阻塞當前線程等待到異步線程提供值。
  • std::future一個實例只能與一個異步線程相關(guān)聯(lián),多個線程則需要使用std::shared_future。

std::promise

成員函數(shù):

名稱作用
operator=從另一個 std::promise 移動到當前對象。
swap()交換移動兩個 std::promise。
get_future()獲取與其管理的std::future
set_value()設(shè)置共享狀態(tài)值,此后promise共享狀態(tài)標識變?yōu)閞eady
set_value_at_thread_exit()設(shè)置共享狀態(tài)的值,但是不將共享狀態(tài)的標志設(shè)置為 ready,當線程退出時該 promise 對象會自動設(shè)置為 ready
set_exception()設(shè)置異常,此后promise的共享狀態(tài)標識變?yōu)閞eady
set_exception_at_thread_exit()設(shè)置異常,但是到該線程結(jié)束時才會發(fā)出通知

1、std::promise::get_future:返回一個與promise共享狀態(tài)相關(guān)聯(lián)的future對象

2、std::promise::set_value:設(shè)置共享狀態(tài)的值,此后promise共享狀態(tài)標識變?yōu)閞eady

3、std::promise::set_exception:為promise設(shè)置異常,此后promise的共享狀態(tài)標識變?yōu)閞eady

4、std::promise::set_value_at_thread_exit:設(shè)置共享狀態(tài)的值,但是不將共享狀態(tài)的標志設(shè)置為 ready,當線程退出時該 promise 對象會自動設(shè)置為 ready(注意:該線程已設(shè)置promise的值,如果在線程結(jié)束之后有其他修改共享狀態(tài)值的操作,會拋出future_error(promise_already_satisfied)異常)

5、std::promise::swap:交換 promise 的共享狀態(tài)

  • std::promise的set相關(guān)函數(shù)和get_future()只能被調(diào)用一次,多次調(diào)用會拋出異常
  • std::promise作為使用者的異步線程中應(yīng)當注意到共享變量的生命周期、是否被set問題。如果沒被set而線程就結(jié)束了,future端就會拋出異常。

多線程std::shared_future

std::future 有個非常明顯的問題,就是只能和一個 std::promise 成對綁定使用,也就意味著僅限于兩個線程之間使用。

那么多個線程是否可以呢,可以!就是 std::shared_future。

std::shared_future 也是一個模板類,它的功能定位、函數(shù)接口和 std::future 一致,不同的是它允許給多個線程去使用,讓多個線程去同步、共享:

它的語法是:

【語法】【偽代碼】std::shared_future<Type> s_fu(pt.get_future());

#include <iostream>
#include <future>
#include <thread>

using namespace std;
using namespace std::chrono;

void futureHandleEntry(std::shared_future<int>& future) 
{
	if (future.valid()) {
		future.wait();
		std::cout << "thread:[" << std::this_thread::get_id() << "] value=" << future.get() << std::endl;
		std::cout << "thread:[" << std::this_thread::get_id() << "] quit!!!" << std::endl;
	}
	else {
		std::cout << "future is invalid!!!" << std::endl;
	}
}

int main()
{
    std::promise<int> promise;
	//獲取shared_future用于多線程訪問異步操作結(jié)果
	std::shared_future<int> future = promise.get_future();

	std::thread t1(&futureHandleEntry, ref(future));
	std::thread t2(&futureHandleEntry, ref(future));
	std::thread t3(&futureHandleEntry, ref(future));

	std::cout << "main thread running!!!" << std::endl;
	//主線程sleep5s后去set_value
	std::this_thread::sleep_for(std::chrono::seconds(5));
	promise.set_value(10);

	t1.join();
	t2.join();
	t3.join();
}

promise和future進階

我們知道異常的場景:

1、當重復(fù)調(diào)用promise的set_value會導(dǎo)致拋出異常

#include <iostream>
#include <thread>
#include <future>
#include <chrono>

using namespace std;

void threadEntry(std::future<int>& future)
{
	try {
		auto value = future.get();
		std::cout << "value=" << value << std::endl;
	}
	catch (std::future_error& error) {
		std::cerr << error.code() << "\n" << error.what() << std::endl;
	}
}
int main()
{
	std::promise<int> promise;
	std::future<int> future = promise.get_future();

	std::thread t1(threadEntry, ref(future));
	/*主線程promise多次調(diào)用set_value會拋出future_error異常
	*/
	std::this_thread::sleep_for(std::chrono::seconds(2));
	promise.set_value(1); 
	promise.set_value(1);

	t1.join();
}

在linux中運行結(jié)果如下: 會有Promise already satisfied的錯誤提示

2、 當std::promise不設(shè)置值時線程就退出

如果promise直到銷毀時,都未設(shè)置過任何值,則promise會在析構(gòu)時自動設(shè)置為std::future_error,這會造成std::future.get拋出std::future_error異常。

#include <iostream> // std::cout, std::endl
#include <thread>   // std::thread
#include <future>   // std::promise, std::future
#include <chrono>   // seconds
using namespace std::chrono;

void read(std::future<int> future) {
    try {
        future.get();
    } catch(std::future_error &e) {
        std::cerr << e.code() << "\n" << e.what() << std::endl;
    }
}

int main() {
    std::thread thread;
    {
        // 如果promise不設(shè)置任何值
        // 則在promise析構(gòu)時會自動設(shè)置為future_error
        // 這會造成future.get拋出該異常
        std::promise<int> promise;
        thread = std::thread(read, promise.get_future());
   }
    thread.join();

    return 0;
}

3、通過std::promise讓std::future拋出異常

通過std::promise.set_exception函數(shù)可以設(shè)置自定義異常,該異常最終會被傳遞到std::future,并在其get函數(shù)中被拋出。

#include <iostream>
 #include <future>
 #include <thread>
 #include <exception>  // std::make_exception_ptr
 #include <stdexcept>  // std::logic_error
 
void catch_error(std::future<void> &future) {
    try {
       future.get();
    } catch (std::logic_error &e) {
       std::cerr << "logic_error: " << e.what() << std::endl;
   }
}

int main() {
    std::promise<void> promise;
    std::future<void> future = promise.get_future();

    std::thread thread(catch_error, std::ref(future));
    // 自定義異常需要使用make_exception_ptr轉(zhuǎn)換一下
    promise.set_exception(
       std::make_exception_ptr(std::logic_error("caught")));

      thread.join();
     return 0;
}

std::promise雖然支持自定義異常,但它并不直接接受異常對象:

// std::promise::set_exception函數(shù)原型
2void set_exception(std::exception_ptr p);

自定義異??梢酝ㄟ^位于頭文件exception下的std::make_exception_ptr函數(shù)轉(zhuǎn)化為std::exception_ptr

std::promise

通過上面的例子,我們看到std::promise<void>

是合法的。此時std::promise.set_value不接受任何參數(shù),僅用于通知關(guān)聯(lián)的std::future.get()解除阻塞。

std::promise所在線程退出時

std::async(異步運行)時,開發(fā)人員有時會對std::promise所在線程退出時間比較關(guān)注。std::promise支持定制線程退出時的行為:

  • std::promise.set_value_at_thread_exit 線程退出時,std::future收到通過該函數(shù)設(shè)置的值
  • std::promise.set_exception_at_thread_exit 線程退出時,std::future則拋出該函數(shù)指定的異常。

到此這篇關(guān)于深入了解C++11中promise和future的使用的文章就介紹到這了,更多相關(guān)C++ promise future內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++深度優(yōu)先搜索的實現(xiàn)方法

    C++深度優(yōu)先搜索的實現(xiàn)方法

    這篇文章主要介紹了C++深度優(yōu)先搜索的實現(xiàn)方法,是數(shù)據(jù)結(jié)構(gòu)中非常重要的一種算法,需要的朋友可以參考下
    2014-08-08
  • 基于ios中的流狀態(tài)的定義分析

    基于ios中的流狀態(tài)的定義分析

    本篇文章介紹了,基于ios中的流狀態(tài)的定義分析。需要的朋友參考下
    2013-05-05
  • C++可執(zhí)行文件絕對路徑值與VS安全檢查詳解

    C++可執(zhí)行文件絕對路徑值與VS安全檢查詳解

    這篇文章主要給大家介紹了關(guān)于C++可執(zhí)行文件絕對路徑值與VS安全檢查的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-01-01
  • opencv檢測直線方法之形態(tài)學(xué)方法

    opencv檢測直線方法之形態(tài)學(xué)方法

    這篇文章主要為大家詳細介紹了opencv檢測直線方法之形態(tài)學(xué)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • C語言 函數(shù)缺省參數(shù)詳情

    C語言 函數(shù)缺省參數(shù)詳情

    這篇文章主要介紹了C語言 的函數(shù)缺省參數(shù)、除了介紹函數(shù)全缺省參數(shù),和半缺省參數(shù)還簡單介紹了函數(shù)聲明、函數(shù)調(diào)用等一些函數(shù)的定義,需要的朋友可以參考下面文章內(nèi)容
    2021-09-09
  • C++中map和set的使用詳細攻略

    C++中map和set的使用詳細攻略

    set set是一種關(guān)聯(lián)式容器,下面這篇文章主要給大家介紹了關(guān)于C++中map和set使用的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-02-02
  • C語言中那些你必須知道的常用關(guān)鍵字

    C語言中那些你必須知道的常用關(guān)鍵字

    這篇文章主要介紹了C語言中我們常用的關(guān)鍵字靜態(tài)static的詳細講解和typedef?、#define定義常量和宏,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • C++?opencv實現(xiàn)在圖片上畫一條線示例代碼

    C++?opencv實現(xiàn)在圖片上畫一條線示例代碼

    這篇文章主要為大家介紹了C++?opencv實現(xiàn)在圖片上畫一條線的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 詳解state狀態(tài)模式及在C++設(shè)計模式編程中的使用實例

    詳解state狀態(tài)模式及在C++設(shè)計模式編程中的使用實例

    這篇文章主要介紹了state狀態(tài)模式及在C++設(shè)計模式編程中的使用實例,在設(shè)計模式中策略用來處理算法變化,而狀態(tài)則是透明地處理狀態(tài)變化,需要的朋友可以參考下
    2016-03-03
  • C語言:利用指針編寫程序,用梯形法計算給定的定積分實例

    C語言:利用指針編寫程序,用梯形法計算給定的定積分實例

    今天小編就為大家分享一篇C語言:利用指針編寫程序,用梯形法計算給定的定積分實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評論