GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::this_thread::yield(3) C++ Standard Libary std::this_thread::yield(3)

std::this_thread::yield - std::this_thread::yield


Defined in header <thread>
void yield() noexcept; (since C++11)


Provides a hint to the implementation to reschedule the execution of threads,
allowing other threads to run.


(none)


(none)


The exact behavior of this function depends on the implementation, in particular on
the mechanics of the OS scheduler in use and the state of the system. For example, a
first-in-first-out realtime scheduler (SCHED_FIFO in Linux) would suspend the
current thread and put it on the back of the queue of the same-priority threads that
are ready to run (and if there are no other threads at the same priority, yield has
no effect).

// Run this code


#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

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.