名前空間
変種
操作

「cpp/thread/yield」の版間の差分

提供: cppreference.com
< cpp‎ | thread
(Translated from the English version using Google Translate)
 
 
(3人の利用者による、間の4版が非表示)
1行: 1行:
{{tr_note}}
 
 
{{cpp/title|n=this_thread::|yield}}
 
{{cpp/title|n=this_thread::|yield}}
 
{{cpp/thread/navbar}}
 
{{cpp/thread/navbar}}
{{ddcl | header=thread | notes={{mark since c++11}} | 1=
+
{{ddcl | header=thread | sincec++11 | 1=
void yield();
+
void yield() ;
 
}}
 
}}
  
{{tr|他のスレッドが実行できるように、スレッドの実行をスケジュール変更するヒントを実装に提供. |Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run. }}
+
  
===パラメータ===
+
======
{{tr|(なし)|(none)}}
+
()
  
===値を返します===
+
======
{{tr|(なし)|(none)}}
+
 +
 
 +
 +
{{|) }}
  
 
===例===
 
===例===
47行: 49行:
 
waited for 128 microseconds
 
waited for 128 microseconds
 
}}
 
}}
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +

2018年4月3日 (火) 14:16時点における最新版

 
 
スレッドサポートライブラリ
スレッド
(C++11)
(C++20)
(C++20)
this_thread 名前空間
(C++11)
yield
(C++11)
(C++11)
相互排他
(C++11)
汎用ロック管理
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
条件変数
(C++11)
セマフォ
ラッチとバリア
(C++20)
(C++20)
フューチャー
(C++11)
(C++11)
(C++11)
(C++11)
 
ヘッダ <thread> で定義
void yield() noexcept;
(C++11以上)

他のスレッドが実行できるように、スレッドの実行を再スケジュールするためのヒントを処理系に提供します。

目次

[編集] 引数

(なし)

[編集] 戻り値

(なし)

[編集] ノート

この関数の正確な動作は処理系に、特に使用中の OS のスケジューラの機構やシステムの状態に依存します。 例えば、先入れ先出しのリアルタイムスケジューラ (Linux の SCHED_FIFO) は、現在のスレッドをサスペンドし、同じ優先度の実行可能なスレッドのキューの末尾に置くでしょう (そしてもし、同じ優先度のスレッドが他になければ、 yield は効果を持ちません)。

[編集]

#include <iostream>
#include <chrono>
#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

[編集] 関連項目