溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++11中promise future如何使用

發(fā)布時間:2021-07-12 09:32:42 來源:億速云 閱讀:245 作者:Leah 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)C++11中promise future如何使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

std::promise 類介紹

promise 對象可以保存某一類型 T 的值,該值可被 future 對象讀?。赡茉诹硗庖粋€線程中),因此 promise 也提供了一種線程同步的手段。在 promise 對象構(gòu)造時可以和一個共享狀態(tài)(通常是std::future)相關(guān)聯(lián),并可以在相關(guān)聯(lián)的共享狀態(tài)(std::future)上保存一個類型為 T 的值。

可以通過 get_future 來獲取與該 promise 對象相關(guān)聯(lián)的 future 對象,調(diào)用該函數(shù)之后,兩個對象共享相同的共享狀態(tài)(shared state)

  • promise 對象是異步 Provider,它可以在某一時刻設(shè)置共享狀態(tài)的值。

  • future 對象可以異步返回共享狀態(tài)的值,或者在必要的情況下阻塞調(diào)用者并等待共享狀態(tài)標(biāo)志變?yōu)?ready,然后才能獲取共享狀態(tài)的值。

下面以一個簡單的例子來說明上述關(guān)系

C++11中promise future如何使用

#include <iostream>       // std::cout#include <functional>     // std::ref#include <thread>         // std::thread#include <future>         // std::promise, std::futurevoid print_int(std::future<int>& fut) {int x = fut.get(); // 獲取共享狀態(tài)的值.std::cout << "value: " << x << '\n'; // 打印 value: 10.}int main ()
{
    std::promise<int> prom; // 生成一個 std::promise<int> 對象.std::future<int> fut = prom.get_future(); // 和 future 關(guān)聯(lián).std::thread t(print_int, std::ref(fut)); // 將 future 交給另外一個線程t.prom.set_value(10); // 設(shè)置共享狀態(tài)的值, 此處和線程t保持同步.    t.join();return 0;
}

std::promise 構(gòu)造函數(shù)

default (1)
promise();
with allocator (2)
template <class Alloc> promise (allocator_arg_t aa, const Alloc& alloc);
copy [deleted] (3)
promise (const promise&) = delete;
move (4)
promise (promise&& x) noexcept;
  1. 默認(rèn)構(gòu)造函數(shù),初始化一個空的共享狀態(tài)。

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

  3. 拷貝構(gòu)造函數(shù),被禁用。

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

另外,std::promise 的 operator= 沒有拷貝語義,即 std::promise 普通的賦值操作被禁用,operator= 只有 move 語義,所以 std::promise 對象是禁止拷貝的。

例子:

C++11中promise future如何使用

#include <iostream>       // std::cout#include <thread>         // std::thread#include <future>         // std::promise, std::futurestd::promise<int> prom;void print_global_promise () {
    std::future<int> fut = prom.get_future();int x = fut.get();
    std::cout << "value: " << x << '\n';
}int main ()
{
    std::thread th2(print_global_promise);
    prom.set_value(10);
    th2.join();

    prom = std::promise<int>();    // prom 被move賦值為一個新的 promise 對象.std::thread th3 (print_global_promise);
    prom.set_value (20);
    th3.join();  return 0;
}

復(fù)制代碼

 std::promise::get_future 介紹

該函數(shù)返回一個與 promise 共享狀態(tài)相關(guān)聯(lián)的 future 。返回的 future 對象可以訪問由 promise 對象設(shè)置在共享狀態(tài)上的值或者某個異常對象。只能從 promise 共享狀態(tài)獲取一個 future 對象。在調(diào)用該函數(shù)之后,promise 對象通常會在某個時間點準(zhǔn)備好(設(shè)置一個值或者一個異常對象),如果不設(shè)置值或者異常,promise 對象在析構(gòu)時會自動地設(shè)置一個 future_error 異常(broken_promise)來設(shè)置其自身的準(zhǔn)備狀態(tài)。上面的例子中已經(jīng)提到了 get_future,此處不再重復(fù)。

std::promise::set_value 介紹
generic template (1)
void set_value (const T& val);
void set_value (T&& val);
specializations (2)
void promise<R&>::set_value (R& val);   // when T is a reference type (R&)
void promise<void>::set_value (void);   // when T is void

設(shè)置共享狀態(tài)的值,此后 promise 的共享狀態(tài)標(biāo)志變?yōu)?ready.

 std::promise::set_exception 介紹

為 promise 設(shè)置異常,此后 promise 的共享狀態(tài)變標(biāo)志變?yōu)?ready,例子如下,線程1從終端接收一個整數(shù),線程2將該整數(shù)打印出來,如果線程1接收一個非整數(shù),則為 promise 設(shè)置一個異常(failbit) ,線程2 在std::future::get 是拋出該異常。

C++11中promise future如何使用

#include <iostream>       // std::cin, std::cout, std::ios#include <functional>     // std::ref#include <thread>         // std::thread#include <future>         // std::promise, std::future#include <exception>      // std::exception, std::current_exceptionvoid get_int(std::promise<int>& prom) {int x;
    std::cout << "Please, enter an integer value: ";
    std::cin.exceptions (std::ios::failbit);   // throw on failbittry {
        std::cin >> x;                         // sets failbit if input is not int        prom.set_value(x);
    } catch (std::exception&) {
        prom.set_exception(std::current_exception());
    }
}void print_int(std::future<int>& fut) {try {int x = fut.get();
        std::cout << "value: " << x << '\n';
    } catch (std::exception& e) {
        std::cout << "[exception caught: " << e.what() << "]\n";
    }
}int main ()
{
    std::promise<int> prom;
    std::future<int> fut = prom.get_future();

    std::thread th2(get_int, std::ref(prom));
    std::thread th3(print_int, std::ref(fut));

    th2.join();
    th3.join();return 0;
}

std::promise::set_value_at_thread_exit 介紹

設(shè)置共享狀態(tài)的值,但是不將共享狀態(tài)的標(biāo)志設(shè)置為 ready,當(dāng)線程退出時該 promise 對象會自動設(shè)置為 ready。如果某個 std::future 對象與該 promise 對象的共享狀態(tài)相關(guān)聯(lián),并且該 future 正在調(diào)用 get,則調(diào)用 get 的線程會被阻塞,當(dāng)線程退出時,調(diào)用 future::get 的線程解除阻塞,同時 get 返回 set_value_at_thread_exit 所設(shè)置的值。注意,該函數(shù)已經(jīng)設(shè)置了 promise 共享狀態(tài)的值,如果在線程結(jié)束之前有其他設(shè)置或者修改共享狀態(tài)的值的操作,則會拋出 future_error( promise_already_satisfied )。

關(guān)于C++11中promise future如何使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI