c++ 之 std::this_thread::yield 與std::this_thread::sleep_for
阿新 • • 發佈:2018-12-03
std::this_thread::yield: 當前執行緒放棄執行,作業系統排程另一執行緒繼續執行。即當前執行緒將未使用完的“CPU時間片”讓給其他執行緒使用,等其他執行緒使用完後再與其他執行緒一起競爭"CPU"。
std::this_thread::sleep_for: 表示當前執行緒休眠一段時間,休眠期間不與其他執行緒競爭CPU,根據執行緒需求,等待若干時間。
this_thread 包裝了一組可以訪問當前執行緒資訊的函式
1、get_id() 獲取當前執行緒的id。
// thread::get_id / this_thread::get_id #include <iostream> // std::cout #include <thread> // std::thread, std::thread::id, std::this_thread::get_id #include <chrono> // std::chrono::seconds std::thread::id main_thread_id = std::this_thread::get_id(); void is_main_thread() { if ( main_thread_id == std::this_thread::get_id() ) std::cout << "This is the main thread.\n"; else std::cout << "This is not the main thread.\n"; } int main() { is_main_thread(); std::thread th (is_main_thread); th.join(); }
輸出結果
This is the main thread.
This is not the main thread.
2、yield()
呼叫執行緒放棄執行,回到準備狀態,重新分配cpu資源。所以呼叫該方法後,可能執行其他執行緒,也可能還是執行該執行緒
// this_thread::yield example #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::yield #include <atomic> // std::atomic std::atomic<bool> ready (false); void count1m(int id) { while (!ready) { // wait until main() sets ready... std::this_thread::yield(); } for (volatile int i=0; i<1000000; ++i) {} std::cout << id; } int main () { std::thread threads[10]; std::cout << "race of 10 threads that count to 1 million:\n"; for (int i=0; i<10; ++i) threads[i]=std::thread(count1m,i); ready = true; // go! for (auto& th : threads) th.join(); std::cout << '\n'; return 0; }
輸出結果
race of 10 threads that count to 1 million...
6189370542
3、template <class Clock, class Duration>
void sleep_until (const chrono::time_point<Clock,Duration>& abs_time);
阻塞呼叫執行緒,一直到指定事件
// this_thread::sleep_for example #include <iostream> // std::cout #include <iomanip> // std::put_time #include <thread> // std::this_thread::sleep_until #include <chrono> // std::chrono::system_clock #include <ctime> // std::time_t, std::tm, std::localtime, std::mktime int main() { using std::chrono::system_clock; std::time_t tt = system_clock::to_time_t (system_clock::now()); struct std::tm * ptm = std::localtime(&tt); std::cout << "Current time: " << std::put_time(ptm,"%X") << '\n'; std::cout << "Waiting for the next minute to begin...\n"; ++ptm->tm_min; ptm->tm_sec=0; std::this_thread::sleep_until (system_clock::from_time_t (mktime(ptm))); std::cout << std::put_time(ptm,"%X") << " reached!\n"; return 0; }
輸出結果
Current time: 11:52:36
Waiting for the next minute to begin...
11:53:00 reached!
4、template <class Rep, class Period>
void sleep_for (const chrono::duration<Rep,Period>& rel_time);
阻塞呼叫執行緒,一直到指定時間段後。
// this_thread::sleep_for example
#include <iostream> // std::cout
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
int main()
{
std::cout << "countdown:\n";
for (int i=10; i>0; --i) {
std::cout << i << '\n';
std::this_thread::sleep_for (std::chrono::seconds(1));
}
std::cout << "Lift off!\n";
return 0;
}
輸出結果
countdown:
10
9
8
7
6
5
4
3
2
1
Lift off!
參考連結:
https://en.cppreference.com/w/cpp/thread/sleep_for