std::async
提供: cppreference.com
![]() |
このページは、Google 翻訳を使って英語版から機械翻訳されました。
翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <future>
|
||
template< class Function, class... Args> std::future<typename std::result_of<Function(Args...)>::type> |
(1) | (C++11およびそれ以降) |
template< class Function, class... Args > std::future<typename std::result_of<Function(Args...)>::type> |
(2) | (C++11およびそれ以降) |
テンプレート関数
1) async
はf
非同期に(潜在的に別々のスレッドで)関数を実行し、最終的にその関数呼び出しの結果を保持するstd::futureを返します。.Original:
The template function
async
runs the function f
asynchronously (potentially in a separate thread) and returns a std::future that will eventually hold the result of that function call.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
async(std::launch::async | std::launch::deferred, f, args...)と同じ動作をします。言い換えれば、
2) f
は、新しいスレッドで開始することができるまたは得られるstd::futureで値を照会されたときには、同期的に実行することができる.Original:
Behaves the same as async(std::launch::async | std::launch::deferred, f, args...). In other words,
f
may be started in a new thread or it may be run synchronously when the resulting std::future is queried for a value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
引数を持つ関数の呼び出し
f
args
固有の起動ポリシーに従ってpolicy
: Original:
Calls a function
f
with arguments args
according to a specific launch policy policy
: The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 非同期フラグ(policy & std::launch::async != 0をIE)の設定されている場合は、
async
は関数std::thread(f, args...)が値を返すか、例外をスローした場合、つまり、それがアクセス可能な共有状態に格納されている場合を除き、f
たかのように実行の新しいスレッドを生成std::futureが呼び出し元に返すことasync
通じ.Original:If the async flag is set (i.e. policy & std::launch::async != 0), thenasync
spawns a new thread of execution as if by std::thread(f, args...), except that if the functionf
returns a value or throws an exception, it is stored in the shared state accessible through the std::future thatasync
returns to the caller.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- (policy & std::launch::deferred != 0をIE)'繰延フラグが設定されている場合、
async
コンストラクタなどによってその後args...
改宗がstd::thread同じ方法ですが、新しい実行のスレッドを生成しません。その代わりに、遅延評価'は実行されます。std::futureが呼び出し元に返されたasync
上の非時限待機関数の最初の呼び出しをf(args...)
は、現在のスレッドで実行されるようになります。同じstd::futureへのすべてのアクセスがさらに即座に結果を返します。.Original:If the deferred flag is set (i.e. policy & std::launch::deferred != 0), thenasync
convertsargs...
the same way as by std::thread constructor, but does not spawn a new thread of execution. Instead, lazy evaluation is performed: the first call to a non-timed wait function on the std::future thatasync
returned to the caller will causef(args...)
to be executed in the current thread. All further accesses to the same std::future will return the result immediately.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- std::launch::asyncとstd::launch::deferredフラグが両方
policy
に設定されている場合、それは非同期実行または遅延評価を実行するかどうかは実装に任され.Original:If both the std::launch::async and std::launch::deferred flags are set inpolicy
, it is up to the implementation whether to perform asynchronous execution or lazy evaluation.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集] パラメータ
f | - | 関数または関数オブジェクトが呼び出すことができます
Original: function or function object to call The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
args... | - | パラメータ
f に渡すOriginal: parameters to pass to f The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||||||||||||||||||||
policy | - | 個々のビットは、実行の許可されているメソッドを制御するビットマスク値、
Original: bitmask value, where individual bits control the allowed methods of execution
The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[編集] 値を返します
std::future関数の戻り値を参照する.
Original:
std::future referring to the return value of the function.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 例外
起動ポリシーがstd::system_errorであり、実装が新しいスレッドを起動できない場合、エラー条件std::errc::resource_unavailable_try_againとstd::launch::asyncをスロー.
Original:
Throws std::system_error with error condition std::errc::resource_unavailable_try_again if the launch policy is std::launch::async and the implementation is unable to start a new thread.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] ノート
実装では、デフォルトの起動ポリシーで(実装定義)の付加ビットを有効にすることで、std::asyncの最初のオーバーロードの挙動を延長することができる.
Original:
The implementation may extend the behavior of the first overload of std::async by enabling additional (implementation-defined) bits in the default launch policy.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 例
このコードを実行します
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <future> template <typename RAIter> int parallel_sum(RAIter beg, RAIter end) { typename RAIter::difference_type len = end-beg; if(len < 1000) return std::accumulate(beg, end, 0); RAIter mid = beg + len/2; auto handle = std::async(std::launch::async, parallel_sum<RAIter>, mid, end); int sum = parallel_sum(beg, mid); return sum + handle.get(); } int main() { std::vector<int> v(10000, 1); std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n'; }
出力:
The sum is 10000