std::this_thread::yield
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <thread> 中定义
|
||
void yield(); |
(C++11 起) | |
提供一个提示重新安排执行线程的执行,让其他线程运行.
原文:
Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run.
[编辑] 参数
(无)
[编辑] 返回值
(无)
[编辑] 示例
#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"; }
Possible output:
waited for 128 microseconds