Std::this_thread::yield:
defined in the header file<thread>
function prototype :void yield() noexcept;
The accuracy of this function is dependent on implementation, especially in the use of the OS Scheduler mechanism and system State. For example, the FIFO real-time Scheduler (Linux SCHED_FIFO
) hangs the current thread and places it at the end of the queue of the same priority thread that is ready to run (and no effect if no other thread is in the same priority yield
)
Code:
1#include <iostream>2#include <thread>3#include <chrono>4 using namespacestd;5 6 voidLittle_sleep (std::chrono::milliseconds us) {7Auto start =Std::chrono::high_resolution_clock::now ();8Auto End = start +us;9 Do { TenStd::this_thread::yield();//Yield current time slice One} while(Std::chrono::high_resolution_clock::now () <end); A } - - intMainvoid) { theAuto start = Std::chrono::high_resolution_clock::now ();//Get current Time - -Little_sleep (Std::chrono::milliseconds ( -)); - +Auto elapsed = Std::chrono::high_resolution_clock::now ()-Start;//calculate the time spent executing little_sleep - +cout <<"waited fo" A<< Std::chrono::d uration_cast<std::chrono::milliseconds> (elapsed). Count ()//Convert the elapsed time period to milliseconds and output at<<"milliseconds\n"; - - //Output: - //waited fo milliseconds - - return 0; in}
View Code
STD::THIS_THREAD::GET_ID:
defined in the header file<thread>
function prototype :std::thread::ID get_id() noexcept;
Gets the ID of the current thread
Code:
1#include <iostream>2#include <thread>3#include <chrono>4#include <mutex>5 using namespacestd;6 7 Std::mutex G_display_mutex;8 9 voidfoo () {TenAuto this_id =std::this_thread::get_id (); One AG_display_mutex.Lock(); -cout <<"Thread"<< this_id <<"Sleeping ..."<<Endl; - G_display_mutex.unlock (); the -Std::this_thread::sleep_for (Std::chrono::seconds (1)); - } - + intMainvoid) { - std::thread T1 (foo); + std::thread T2 (foo); A at T1.join (); - T2.join (); - - //Output: - //thread2 sleeping ... - //thread3 sleeping ... in - return 0; to}
View Code
Std::this_thread::sleep_for:
defined in the header file<thread>
function Prototypes :
Template< class Rep, class Period >
void Sleep_for( const std::chrono::duration<rep, Period >& sleep_duration );
Blocks the current thread execution, at least for the specified sleep_duration
.
This function may be blocked longer than sleep_duration
because of a delay in scheduling or resource contention.
The standard library recommends measuring the length of time with a stable clock. If the implementation is replaced by system time, then the wait time may also be sensitive to always adjust
Exception: An exception that is thrown by any clock, time_point, or duration during execution (the clock, point-in-time, and duration of the standard library are never thrown)
Code:
1#include <iostream>2#include <chrono>3#include <thread>4 using namespacestd;5 6 intMainvoid) {7cout <<"Hello Waiter"<<Endl;8 9Auto start =Std::chrono::high_resolution_clock::now ();TenStd::this_thread::sleep_for (Std::chrono::seconds (2)); OneAuto End =Std::chrono::high_resolution_clock::now (); A -Std::chrono::d uration<Double, std::milli> elapsed = end-start; -cout <<"waited"<< Elapsed.count () <<"Ms"<<Endl; the - //Output: - //Hello Waiter - //waited 2001.44 Ms + - return 0; +}
View Code
Std::this_thread::sleep_until:
defined in the header file<thread>
Template< class Clock, class Duration >
void Sleep_until( const std::chrono::time_point<clock, Duration>& sleep_time );
Blocks the current thread until it arrives at the specified sleep_time
.
Use sleep_time
the associated clock, which indicates that the clock adjustment has an effect. As a result, the length of the block may be less than, but not more than, sleep_time -Clock: Now() after the call point in time. Function may also be blocked longer than sleep_time
after arrival due to scheduling or resource dispute delays
Code:
1#include <iostream>2#include <chrono>3#include <thread>4 using namespacestd;5 6 intMainvoid) {7Auto start =Std::chrono::high_resolution_clock::now ();8Std::this_thread::sleep_until (Start + std::chrono::seconds (2));9Auto End =Std::chrono::high_resolution_clock::now ();Ten OneStd::chrono::d uration<Double, std::milli> elapsed = end-start; Acout <<"waited"<< Elapsed.count () <<"Ms"<<Endl; - - //Output: the //waited 2001.42 Ms - - return 0; -}
View Code
C + + multithreaded base 2 (namespace This_thread)