1. 程式人生 > >【C++並發實戰】(三) std::future和std::promise

【C++並發實戰】(三) std::future和std::promise

指向 code 實例 value 互斥 stdfuture 通過 模板 som

std::future和std::promise

std::future

std::future期待一個返回,從一個異步調用的角度來說,future更像是執行函數的返回值,C++標準庫使用std::future為一次性事件建模,如果一個事件需要等待特定的一次性事件,那麽這線程可以獲取一個future對象來代表這個事件。
異步調用往往不知道何時返回,但是如果異步調用的過程需要同步,或者說後一個異步調用需要使用前一個異步調用的結果。這個時候就要用到future。
線程可以周期性的在這個future上等待一小段時間,檢查future是否已經ready,如果沒有,該線程可以先去做另一個任務,一旦future就緒,該future就無法復位(無法再次使用這個future等待這個事件),所以future代表的是一次性事件

future的類型

<future>庫的頭文件中聲明了兩種future,唯一future(std::future)和共享future(std::shared_future)這兩個是參照std::unique_ptr和std::shared_ptr設立的,前者的實例是僅有的一個指向其關聯事件的實例,而後者可以有多個實例指向同一個關聯事件,當事件就緒時,所有指向同一事件的std::shared_future實例會變成就緒。

future的使用

std::future是一個模板,例如std::future<int>,模板參數就是期待返回的類型,雖然future被用於線程間通信,但其本身卻並不提供同步訪問,熱門必須通過互斥元或其他同步機制來保護訪問。
future使用的時機是當你不需要立刻得到一個結果的時候,你可以開啟一個線程幫你去做一項任務,並期待這個任務的返回,但是std::thread並沒有提供這樣的機制,這就需要用到std::async和std::future(都在<future>

頭文件中聲明)
std::async返回一個std::future對象,而不是給你一個確定的值(所以當你不需要立刻使用此值的時候才需要用到這個機制)。當你需要使用這個值的時候,對future使用get(),線程就會阻塞直到future就緒,然後返回該值。

#include <future>
#include <iostream>

int find_result_to_add()
{
    return 1 + 1;
}

void do_other_things() 
{
    std::cout << "Hello World" << std::endl;
}

int main()
{
    std::future<int> result = std::async(find_result_to_add);
    do_other_things();
    std::cout << result.get() << std::endl;
    return 0;
}

跟thread類似,async允許你通過將額外的參數添加到調用中,來將附加參數傳遞給函數。如果傳入的函數指針是某個類的成員函數,則還需要將類對象指針傳入(直接傳入,傳入指針,或者是std::ref封裝)。
默認情況下,std::async是否啟動一個新線程,或者在等待future時,任務是否同步運行都取決於你給的參數。這個參數為std::launch類型

  • std::launch::defered表明該函數會被延遲調用,直到在future上調用get()或者wait()為止
  • std::launch::async,表明函數會在自己創建的線程上運行
  • std::launch::any = std::launch::defered | std::launch::async
  • std::launch::sync = std::launch::defered
enum class launch
{
    async,deferred,sync=deferred,any=async|deferred
};

PS:默認選項參數被設置為std::launch::any。如果函數被延遲運行可能永遠都不會運行。

std::packaged_task

如果說std::async和std::feature還是分開看的關系的話,那麽std::packaged_task就是將任務和feature綁定在一起的模板,是一種封裝對任務的封裝。

The class template std::packaged_task wraps any Callable target (function, lambda expression, bind expression, or another function object) so that it can be invoked asynchronously. Its return value or exception thrown is stored in a shared state which can be accessed through std::future objects.

可以通過std::packaged_task對象獲取任務相關聯的feature,調用get_future()方法可以獲得std::packaged_task對象綁定的函數的返回值類型的future。std::packaged_task的模板參數是函數簽名
PS:例如int add(int a, intb)的函數簽名就是int(int, int)

#include <future>
#include <iostream>

int add(int a, int b)
{
    return a + b;
}

void do_other_things() 
{
    std::cout << "Hello World" << std::endl;
}

int main()
{
    std::packaged_task<int(int, int)> task(add);
    do_other_things();
    std::future<int> result = task.get_future();
    task(1, 1); //必須要讓任務執行,否則在get()獲取future的值時會一直阻塞
    std::cout << result.get() << std::endl;
    return 0;
}

std::promise

從字面意思上理解promise代表一個承諾。promise比std::packaged_task抽象層次低。
std::promise<T>提供了一種設置值的方式,它可以在這之後通過相關聯的std::future<T>對象進行讀取。換種說法,之前已經說過std::future可以讀取一個異步函數的返回值了,那麽這個std::promise就提供一種方式手動讓future就緒。

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

void print(std::promise<std::string>& p)
{
    p.set_value("There is the result whitch you want.");
}

void do_some_other_things()
{
    std::cout << "Hello World" << std::endl;
}

int main()
{
    std::promise<std::string> promise;

    std::future<std::string> result = promise.get_future();
    std::thread t(print, std::ref(promise));
    do_some_other_things();
    std::cout << result.get() << std::endl;
    t.join();
    return 0;
}

由此可以看出在promise創建好的時候future也已經創建好了
線程在創建promise的同時會獲得一個future,然後將promise傳遞給設置他的線程,當前線程則持有future,以便隨時檢查是否可以取值。

總結

future的表現為期望,當前線程持有future時,期望從future獲取到想要的結果和返回,可以把future當做異步函數的返回值。而promise是一個承諾,當線程創建了promise對象後,這個promise對象向線程承諾他必定會被人設置一個值,和promise相關聯的future就是獲取其返回的手段。

【C++並發實戰】(三) std::future和std::promise