C ++同時実行ガイド<future>(1)std :: promise

<future>ヘッダーファイルには、次のクラスと関数が含まれています。

  1. プロバイダークラス:std :: promise、std :: package_task
  2. 先物クラス:std :: future、shared_future。
  3. プロバイダー関数:std :: async()
  4. 円別クラス型:std :: future_error、std :: future_errc、std :: future_status、
    std :: launch。

std :: promiseクラスの概要

promiseオブジェクトは、特定のタイプTの値を格納できます。この値は、将来のオブジェクト(おそらく別のスレッド)で読み取ることができるため、promiseはスレッド同期の手段も提供します。promiseオブジェクトが作成されると、共有状態(通常はstd :: future)に関連付けることができ、タイプTの値を関連付けられた共有状態(std :: future)に格納できます。
get_futureを使用して、promiseオブジェクトに関連付けられたfutureオブジェクトを取得できます。この関数を呼び出した後、2つのオブジェクトは同じ共有状態を共有します。

  • promiseオブジェクトは非同期プロバイダーであり、特定の時点で共有状態の値を設定できます。
  • futureオブジェクトは、共有状態の値を非同期で返すか、必要に応じて、呼び出し元をブロックし、共有状態フラグの準備が整うのを待ってから、共有状態の値を取得できます。

上記の関係を説明する簡単な例を次に示します。

#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

void print_int(std::future<int>& fut) {
    
    
    int x = fut.get(); // 获取共享状态的值.
    std::cout << "value: " << x << '\n'; // 打印 value: 10.
}

int main ()
{
    
    
    std::promise<int> prom; // 生成一个 std::promise<int> 对象.
    std::future<int> fut = prom.get_future(); // 和 future 关联.
    std::thread t(print_int, std::ref(fut)); // 将 future 交给另外一个线程t.
    
    prom.set_value(10); // 设置共享状态的值, 此处和线程t保持同步.
    std::cout << "set value 10" << std::endl;
    
    t.join();
    
    return 0;
}

std :: promiseコンストラクター

デフォルト 約束する();
アロケーター付き テンプレートpromise(allocator_arg_t aa、const Alloc&alloc);
コピー[削除済み] promise(const promise&)=削除;
移動する promise(promise && x)noexcept;
  1. デフォルトのコンストラクターは、空の共有状態を初期化します。
  2. カスタムメモリアロケータを備えたコンストラクタは、デフォルトのコンストラクタに似ていますが、カスタムアロケータを使用して共有状態を割り当てます。
  3. コピーコンストラクタは無効になっています。
  4. コンストラクターを移動します。

さらに、std :: promiseのoperator =にはコピーセマンティクスがありません。つまり、std :: promiseの通常の代入操作は無効であり、operator =には移動セマンティクスしかないため、std :: promiseオブジェクトのコピーは禁止されています。

例:

#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

std::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 th1(print_global_promise);
    prom.set_value(10);
    th1.join();

    prom = std::promise<int>();    // prom 被move赋值为一个新的 promise 对象.

    std::thread th2 (print_global_promise);
    prom.set_value (20);
    th2.join();

    return 0;
}

std :: promise :: get_future介绍

この関数は、promiseの共有状態に関連付けられたfutureを返します。返されたfutureオブジェクトは、promiseオブジェクトまたは例外オブジェクトによって共有状態に設定された値にアクセスできます。promise共有状態から取得できるfutureオブジェクトは1つだけです。この関数を呼び出した後、promiseオブジェクトは通常特定の時点で準備ができています(値または例外オブジェクトを設定します)。値または例外が設定されていない場合、promiseオブジェクトはfuture_error例外(broken_promise)を自動的に設定します。独自の準備完了状態を設定するために破棄されます。get_futureは上記の例で言及されているため、ここでは繰り返しません。

std :: promise :: set_valueの概要

共有状態の値を設定します。その後、promiseの共有状態フラグの準備が整います。

std :: promise :: set_exceptionの概要

promiseの例外を設定します。その後、promiseの共有状態の準備が整います。たとえば、スレッド1は端末から整数を受け取り、スレッド2は整数を出力します。スレッド1が非整数を受け取った場合は、次のように設定します。 promise(failbit)の例外である場合、スレッド2はstd :: future :: getで例外をスローします。

#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_exception

void get_int(std::promise<int>& prom) {
    
    
    int x;
    std::cout << "Please, enter an integer value: ";
    std::cin.exceptions (std::ios::failbit);   // throw on failbit
    try {
    
    
        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 th1(get_int, std::ref(prom));
    std::thread th2(print_int, std::ref(fut));
    std::thread th1(get_int, std::ref(prom));

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

std :: promise :: set_value_at_thread_exit介绍

共有状態の値を設定しますが、共有状態のフラグをreadyに設定しないでください。スレッドが終了すると、promiseオブジェクトは自動的にreadyに設定されます。std :: futureオブジェクトがpromiseオブジェクトの共有状態に関連付けられており、futureがgetを呼び出している場合、getを呼び出すスレッドはブロックされます。スレッドが終了すると、future :: getを呼び出すスレッドのブロックが解除されます。同時に、getはset_value_at_thread_exitによって設定された値を返します。この関数はすでにpromise共有状態の値を設定していることに注意してください。スレッドが終了する前に共有状態の値を設定または変更する他の操作がある場合、future_error(promise_already_satisfied)がスローされます。

std :: promise :: swapの紹介

約束の共有状態を交換します。

おすすめ

転載: blog.csdn.net/qq_24649627/article/details/114139029