「std::packaged task」の版間の差分

提供: C++入門
移動: 案内検索
(ページの作成:「std::packaged_task とは、C++で非同期処理を実現するための機能の1つです。タスク(スレッド)を作成して、std::futureを通...」)
 
(ソースコード std_packaged_task1.cpp)
行15: 行15:
 
* [[std::thread]]を用いた例
 
* [[std::thread]]を用いた例
 
=== ソースコード std_packaged_task1.cpp ===
 
=== ソースコード std_packaged_task1.cpp ===
 +
* [[std::pow]]
 
<syntaxhighlight lang="cpp">
 
<syntaxhighlight lang="cpp">
 
#include <iostream>
 
#include <iostream>
行65: 行66:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
=== コンパイル ===
 
=== コンパイル ===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">

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

std::packaged_task とは、C++で非同期処理を実現するための機能の1つです。タスク(スレッド)を作成して、std::futureを通じて、処理の結果(関数の戻り値)を取得できます。

読み方

std::packaged_task
えすてぃーでぃー ぱっけーじど たすく

概要

std::packaged_taskの使い方によっては、std::threadを意識せずに書けます。 std::promiseとは異なり、set_value()で値を書き込む必要はありません。

std::packaged_taskの簡単な例

サンプルでは、以下の種類のstd::packaged_taskの使用方法を取り上げています。

ソースコード std_packaged_task1.cpp

#include <iostream>
#include <future>
#include <thread>
#include <functional>
#include <cmath>
using namespace std;
 
int
foo (int x, int y) {
        return std::pow(x, y);
}
 
void
task_lamba () {
        std::packaged_task<int(int,int)> task1(
                        [](int x, int y) {
                                return std::pow(x,y);
                        }
                        );
        std::future<int> f1 = task1.get_future();
        task1 (2, 4);
        cout << __PRETTY_FUNCTION__ << " " << f1.get() << endl;
}
 
void
task_bind () {
        std::packaged_task<int()> task1(std::bind(foo, 2, 3));
        std::future<int> f1 = task1.get_future();
        task1();
        cout << __PRETTY_FUNCTION__ << " " << f1.get() << endl;
}
 
void
task_thread () {
        std::packaged_task<int(int,int)> task1(foo);
        std::future<int> f1 = task1.get_future();
        std::thread task_td(std::move(task1), 2, 10);
        task_td.join();
        cout << __PRETTY_FUNCTION__ << " " << f1.get() << endl;
}
 
int main(int argc, char const* argv[])
{
        task_lamba();
        task_bind();
        task_thread();
        return 0;
}

コンパイル

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

実行例

% ./std_packaged_task1
void task_lamba() 16
void task_bind() 8
void task_thread() 1024

関連項目

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

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