6.11 std ::非同期深度



6 11 Std Async Depth



https://www.cnblogs.com/pacino12134/p/11270846.html

1. std :: asyncとstd ::スレッドの違い

スレッドはスレッドを作成するように設計されています。システムリソースの制約がある場合、スレッドはスレッドを作成できず、プログラム全体がクラッシュする可能性があります。追加のパラメータなしで非同期呼び出しを実行しても、新しいスレッドは作成されませんが、呼び出されたフォローアップは非同期タスクmythreadがこのgetステートメントがあるスレッドで実行される前に要求する関数(メインでgetを実行し、メインスレッドでmythread関数を呼び出すのと同等)



asyncは通常、非同期タスクを作成するように呼び出されて指示されたスレッドを作成しません。

最も明らかな違い:asyncはスレッドを作成しない場合があります。たとえば、上記のコードは、関数が呼び出された場合にのみスレッドを作成し、それ以外の場合は実行されません。



スレッドの戻り値を取得する場合は、グローバル変数の割り当ても設定する必要があります。スレッド関数の戻り値を簡単に取得できる非同期

-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------

std :: async()とstd :: thread()最も明らかな違いは、非同期であるということです。 必ずしも新しいスレッドを作成する必要はありません



std :: thread()if システムリソースの制約、スレッドの作成に失敗する可能性があり、プログラム全体がクラッシュする可能性があります

std :: thread() スレッドを作成する方法、この値を取得したい場合のスレッドの戻り値は簡単ではありません

std :: async() 非同期タスクを作成します。スレッドを作成する場合と作成しない場合があります。非同期は、スレッドエントリ関数の戻り値を簡単に取得できる方法で呼び出します。

システムリソースの制限による:

(1)std :: thread()を使用して作成されたスレッドが多すぎると、スレッドが作成される可能性があり、失敗し、システムが例外を報告し、クラッシュします

(2)std :: asyncを使用する場合、通常は異常なクラッシュは報告されません。新しいスレッドのときにシステムリソースの制約を作成できない場合、追加のパラメーターなしでstd :: asyncを呼び出すと、新しいスレッドは作成されません。後続のfuture :: get()を呼び出して結果を要求すると、このタスクは、このget()ステートメントの実装が配置されているスレッドで非同期に実行されます。

(3)std :: asyncに新しいスレッドを作成させる場合は、std :: launch :: asyncを使用する必要があります。これは、システムリソースの制約のコストを負担するためであり、プログラムがクラッシュする可能性があります。経験:プログラム、スレッド数は100〜200を超えることは容易ではなく、詳細のタイムスライスはオペレーティングシステムを参照してください。

2.2 std ::非同期パラメータが詳細に入る


パラメータstd :: launch ::パラメータstdの呼び出しを遅らせるために延期:: launch :: asyncは新しいスレッドの作成を強制されました

std :: launch :: deferredを使用して非同期を呼び出す場合はどうなりますか? std :: launch ::呼び出しの遅延、将来のオブジェクト呼び出しの遅延get()またはwait()の実行時にget()またはwait()get()またはwait()を呼び出さない場合、mythread()は実行されません実行されました。


std :: launch :: async:この非同期タスクの実行は新しいスレッドに強制されます。つまり、システムはmythreadを実行するために新しいスレッドを作成する必要があります()


std :: launch :: async | std :: launch :: deferredここで、|:call asyncは、この動作が新しいスレッドを作成してすぐに実行される可能性があると考えているか、delayが新しいスレッドを作成せず、result.get()を呼び出してタスク入力関数の実行を開始しました。どちらか一方。


追加のパラメーターがない場合、関数名への入り口は1つだけで、デフォルトはstd :: launch :: async |である必要があります。 std :: launch :: deferred、およびc)同じ効果、言い換えると、システムは非同期(新しいスレッドを作成)または同期(新しいスレッドを作成しない)の実行を決定します

非同期タスクを作成します。

#include #include using namespace std int mythread(){ cout<<'mythread is begining'<

遅延呼び出し:std ::起動::延期

#include #include using namespace std int mythread(){ cout<<'mythread is begining'<

std :: launch :: asyncスレッドの作成を強制します

#include #include using namespace std int mythread(){ cout<<'mythread is begining'<

std :: launch :: async | std :: launch :: defered

#include #include using namespace std int mythread(){ cout<<'mythread is begining'<

2.3 std :: asyncは不確実性の問題を解決します

追加のパラメーターがない場合、std :: asyncが問題を呼び出し、システムに新しいスレッドを作成するかどうかを決定させます。

std :: future result = std :: async(mythread)の表現に焦点が当てられており、最終的には非同期タスクの実装に遅延が生じます。

コード例:

#include #include #include #include #include #include #include using namespace std int mythread () // thread entry function { cout << 'mythread start' << 'threadid =' << std :: this_thread :: get_id () << endl // print thread id std :: chrono :: milliseconds dura (5000) // set time of 5 seconds std :: this_thread :: sleep_for (dura) // must always rest cout << 'mythread end' << 'threadid =' << std :: this_thread :: get_id () << endl // print thread id return 5 } int main() { cout << 'main' << 'threadid= ' << std::this_thread::get_id() << endl std :: future result = std :: async (mythread) // process is not stuck in here cout << 'continue.....' << endl // enum type std :: future_status status = result.wait_for (std :: chrono :: seconds (0)) // wait for one second if (status == std::future_status::deferred) { // thread execution is delayed, the system resource constraints cout << result.get () << endl // At this point take the call mythread () } else if (status == std::future_status::timeout)// { // Timeout: indicates that the thread execution has not finished I want you to wait for one second, I hope you return, you do not return, then the status = timeout // thread execution has not finished cout << 'Timeout:! indicates that the thread has not executing the' << endl } else if (status == std::future_status::ready) { // represents a thread successfully returns cout << 'thread successfully finished, return!' << endl cout << result.get() << endl } cout << 'I love China!' << endl return 0 }