「std::promise」の版間の差分

提供: C++入門
移動: 案内検索
(ページの作成:「std::promise とは、C++のスレッド用のライブラリです。std::threadstd::futurestd::promiseを使用して、別スレッドの終了...」)
 
行67: 行67:
 
* [[スレッドプログラミング]]
 
* [[スレッドプログラミング]]
 
* [[std::async]]
 
* [[std::async]]
 +
* [[std::promise]]
 +
* [[std::packaged_task]]
 
{{thread}}
 
{{thread}}
 
{{mutex}}
 
{{mutex}}
 
<!-- vim: filetype=mediawiki -->
 
<!-- vim: filetype=mediawiki -->

2014年1月2日 (木) 14:57時点における版

std::promise とは、C++のスレッド用のライブラリです。std::threadstd::futurestd::promiseを使用して、別スレッドの終了を待ち、処理の結果を取得するような非同期処理を実現します。

読み方

std::promise
えすてぃーでぃー ぷろみす

概要

  • 戻り値、または、例外を扱えます。
  • 戻り値の型は、テンプレートで指定します。
  • 例外は、任意の型を扱えます。
  • std::futureは、スレッドの完了を待ちます(同期)。
  • std::fetureオブジェクトは、std::promiseオブジェクトのget_future()で作成します。

std::promisestd::thread で実現する方法と std::async で実現する方法があります。

ヘッダファイル

#include <future>

std::promiseとstd::threadの例

ソースコード std_promise1.cpp

#include <iostream>
#include <future>
#include <thread>
#include <unistd.h>     // sleep
using namespace std;
int
foo (std::promise<int>& pi, int& num) {
        cout << "execute: " << __PRETTY_FUNCTION__ << endl;
        for (int i = 0; i < 10; i++) {
                num++;
                sleep (1);
        }
        pi.set_value(num);
}
int main(int argc, char const* argv[])
{
        try {
                std::promise<int> pi;
                int     num = 0;
                std::thread t1(foo, std::ref(pi), std::ref(num));       // スレッドを開始
                cout << num << endl;
                std::future<int> f0 = pi.get_future();                  // 非同期オブジェクトを受け取る宣言
                int r = f0.get();                                       // 同期でタスク処理を待つ
                cout << r << endl;                                      // 結果を表示
                t1.join();                                              // スレッドの終了を待つ
        } catch (std::exception& e) {
                cerr << e.what() << endl;
        }
        return 0;
}

コンパイル

g++49 -std=c++11 -I/usr/local/lib/gcc49/include/c++/ \
-Wl,-rpath=/usr/local/lib/gcc49  -pthread std_promise1.cpp -o std_promise1

実行例

% ./std_promise1
0
execute: int foo(std::promise<int>&, int&)
10

関連項目

std::thread メンバ関数
関数 説明
メンバ関数
std::thread::thread コンストラクタ。threadオブジェクトを作成します。
std::thread::~thread スレッドがjoinかdetachされている必要があります。スレッドオブジェクトを破棄します。
std::thread::operator= スレッドオブジェクトをmoveします。
オブザーバー
std::thread::joinable スレッドが合流可能であるかチェックします。
std::thread::get_id スレッドのIDを返します。
std::thread::native_handle スレッドハンドルを返します。
std::thread::hardware_concurrency 実装によってサポートされる同時スレッド数を返します。
操作
std::thread::join スレッドの終了を待ちます。
std::thread::detach スレッドハンドルから独立して実行するスレッドを許可します。
std::thread::swap スワップ
非メンバ関数
std::swap スワップ
カレントスレッドの管理
std::this_thread::yield_id 処理系に再スケジュールの機会を与えます。
std::this_thread::get_id スレッドIDを返します。
std::this_thread::sleep_for 指定した時間、現在のスレッドの実行を停止します。
std::this_thread::sleep_until 指定した時刻まで、現在のスレッドの実行を停止します。
mutex
mutexの種類 説明
std::mutex 非再帰的mutex
std::recursive_mutex 再帰的mutext
std::timed_mutex ロック関数でタイムアウトが可能な非再帰的mutex
std::recursive_timed_mutex ロック関数でタイムアウトが可能な再帰的mutex

ロッククラステンプレート