名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | thread
(Translated from the English version using Google Translate)
 
(1版:Translate from the English version)

2012年10月30日 (火) 20:54時点における版

 
 
スレッドサポートライブラリ
スレッド
(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();
(C++11以上)
他のスレッドが実行できるように、スレッドの実行をスケジュール変更するヒントを実装に提供.
Original:
Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

パラメータ

(なし)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

値を返します

(なし)
Original:
(none)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#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