std::this_thread::yield
From cppreference.com
| ヘッダ <thread> で定義 |
||
| void yield() noexcept; |
(C++11以降) | |
実行中のスレッドの実行を再スケジュールするためのヒントを実装に提供し、他のスレッドの実行を可能にします。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
(なし)
[編集] 注記
この関数の正確な動作は、実装、特に使用されているOSスケジューラのメカニズムとシステムの状態によって異なります。たとえば、Linuxのfirst-in-first-outリアルタイムスケジューラ(SCHED_FIFO)は、現在のスレッドを一時停止し、実行可能な同じ優先度のスレッドのキューの末尾に配置します。同じ優先度の他のスレッドがない場合、yield は効果がありません。
[編集] 例
このコードを実行
#include <chrono> #include <iostream> #include <thread> // "busy sleep" while suggesting that other threads run // for a small amount of time void little_sleep(std::chrono::microseconds us) { auto start = std::chrono::high_resolution_clock::now(); auto end = start + us; do { std::this_thread::yield(); } while (std::chrono::high_resolution_clock::now() < end); } int main() { auto start = std::chrono::high_resolution_clock::now(); little_sleep(std::chrono::microseconds(100)); auto elapsed = std::chrono::high_resolution_clock::now() - start; std::cout << "waited for " << std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count() << " microseconds\n"; }
実行結果の例
waited for 128 microseconds
[編集] 関連項目
| C言語のドキュメント (thrd_yield)
|