これは、タスク (引数として渡される) を非同期 (別のスレッドの作成) または同期 (通常の関数の呼び出し) で実行するためc++
に、標準ライブラリによって提供されますAPI
。それは立ち上げ戦略によって異なります。
パラメータ
f
呼び出す呼び出し可能(Callable
)オブジェクトargs
に渡されるf
パラメータpolicy
ビットマスク値。各ビットはどの実行メソッドが許可されるかを制御します。
少し | 説明する |
---|---|
std::launch::async |
非同期評価を有効にし、新しいスレッドを実行してタスクを非同期に実行します。 |
std::launch::deferred |
遅延評価を有効にし、呼び出しスレッドでタスクを実行します (遅延評価)。 future とを呼び出すと実行されますget 。 wait 何も起こらない場合は、実行関数は実行されていません。 |
launch::async|launch::deferred |
これは自動であり、関数は特定の時点で戦略を自動的に選択します。これはシステムとライブラリの実装に依存し、通常はシステムでの同時実行の現在の可用性に合わせて最適化されます。 |
注: ストラテジ引数なしで async(f, 1, 2, 3) を呼び出すと、両方で使用されるストラテジが選択されます。非同期実装では、その戦略を自由に選択できます。これは、タスクが新しいスレッドで実行されるか現在のスレッドで実行されるかがわからないことも意味します。
戦略を開始する
起動戦略は std::async API
の一部であり、最初の引数として渡されます。3 つの起動戦略があります。
- std::launch::async
- std::launch::deferred
- デフォルトの戦略 (std::launch::async | std::launch::deferred)
std::launch::async
関数は別のスレッドとして開始され、リソースが利用できない場合は例外 (std::system_error
エラーstd::resource::unavailable_try_again
) が発生します。
例:
//创建单独的线程
std::future<int>result1=std::async(std::launch::async,AddThread,3,3);
std::launch::deferred
呼び出されたとき、または future
呼び出されたときに、同期呼び出しとして関数を開始します。誰かが遅延戦略を使用して非同期を開始し、 または of を呼び出さなかった場合、関数は決して開始されません。例:get()
wait()
future
get()
wait()
//Using std::launch::deferred policy, no separate thread.
auto result2 = std::async(std::launch::deferred, MulThread, 3, 3);
デフォルトのポリシー
上記の 2 つの戦略を組み合わせたもの、または空白のままにしておきます (以下の例を参照)。関数は非同期 (別のスレッドの作成) または同期 (関数の通常の呼び出し) で開始されます。ライブラリによって異なります。ライブラリは、上記のポリシーの 1 つは、リソースの可用性に関するものです。
例:
//Using Default Policy
//Both below function signature are same, we can use anyone.
std::async(std::launch::async|std::launch::deferred, AddThread, 6, 6);
or
std::async(AddThread, 6, 6);
包括的な例
- 例 1:
#include <future>
#include <iostream>
// 检查它是否是质数
// 参数是必须检查的数字
bool fnprime(int num) {
std::cout << "处理开始。。。请等待。。。\n";
int i = 1;
while (1) {
std::cout << "处理数值" << num << " " << i << std::endl;
i++;
if (i > 100) {
break;
}
}
for (; i < num + 100; ++i) {
// 如果 mod 为0,返回 false,否则返回0
std::cout << "处理数值" << num << " " << i << std::endl;
}
return true;
}
// main method
int main() {
int num = 20;
// 异步调用函数 fnprime ()检查数字是否为质数:
std::future<bool> fobj = std::async(std::launch::async, [&]() -> bool {
std::cout << "处理开始。。。请等待。。。\n";
int i = 1;
while (1) {
std::cout << "处理数值" << num << " " << i << std::endl;
i++;
if (i > 100) {
break;
}
}
for (; i < num + 100; ++i) {
// 如果 mod 为0,返回 false,否则返回0
std::cout << "处理数值" << num << " " << i << std::endl;
}
return true;
});
// 打印该行以显示状态
std::cout << "检查数字4是否为质数 . . \n";
// 等待函数 fnPrime 返回
// bool bobj = fobj.get();
if (true)
std::cout << "给出的数字是质数 . . . ! \n";
else
std::cout << "给出的数字不是质数 . . . ! \n\n";
return 0;
}
g++ -std=c++11 -pthread ...
检查数字4是否为质数 . .
给出的数字是质数 . . . !
处理开始。。。请等待。。。
处理数值20 1
处理数值20 2
...
- 例 2
// library for std::cout
#include <iostream>
// library for std::async and std::future
#include <future>
// library for std::string
#include <string>
std::string samplefunction(const std::string& st) {
return "This is the output of " + st;
}
class SamplefunctionObject {
public:
std::string operator()(const std::string& st) const {
return "This is the output of " + st;
}
};
int main() {
std::cout << std::endl;
// future with the help of function
auto ff = std::async(samplefunction, "sample function");
// future with the help of function object
SamplefunctionObject samplefunctionObject;
auto ffo = std::async(samplefunctionObject, "sample function object");
// future with the help of lambda function
auto fl = std::async(
[](const std::string& st) {
return "This is the output of " + st; },
" lambda function");
std::cout << ff.get() << "\n" << ffo.get() << "\n" << fl.get() << std::endl;
std::cout << std::endl;
}
This is the output of sample function
This is the output of sample function object
This is the output of lambda function