Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/yield"

From cppreference.com
< cpp‎ | thread
m (Text replace - "{{example}}" to "===Example===")
m
 
(24 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
{{cpp/title|n=this_thread::|yield}}
 
{{cpp/title|n=this_thread::|yield}}
{{cpp/thread/sidebar}}
+
{{cpp/thread/navbar}}
{{ddcl | header=thread | notes={{mark c++0x feature}} | 1=
+
{{ddcl|header=thread|since=c++11|1=
void yield;
+
void yield() noexcept;
 
}}
 
}}
  
Hints the implementation to reschedule the execution of threads. Another thread may continue execution after the call to this function.  
+
Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run.  
  
<!-- ======== -->
+
===Parameters===
{{params}}
+
(none)
{{param none}}
+
  
<!-- ======== -->
+
===Return value===
{{returns}}
+
(none)
{{return none}}
+
 
 +
===Notes===
 +
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 ({{tt|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, {{tt|yield}} has no effect.
  
<!-- ======== -->
 
 
===Example===
 
===Example===
{{example cpp |
+
{{example
| code=
+
|code=
| output=
+
#include <chrono>
 +
#include <iostream>
 +
#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";
 +
}
 +
|p=true
 +
|output=
 +
waited for 128 microseconds
 
}}
 
}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/thread/thrd_yield}}
 +
{{dsc end}}
 +
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 02:35, 7 October 2024

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
yield
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
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.

Contents

[edit] Parameters

(none)

[edit] Return value

(none)

[edit] Notes

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.

[edit] Example

#include <chrono>
#include <iostream>
#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

[edit] See also

C documentation for thrd_yield