1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<h1 id="firstHeading" class="firstHeading">thrd_yield</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><threads.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">void thrd_yield(void);</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Provides a hint to the implementation to reschedule the execution of threads, allowing other threads to run.</p>
<h3 id="Parameters"> Parameters</h3> <p>(none)</p>
<h3 id="Return_value"> Return value</h3> <p>(none)</p>
<h3 id="Notes"> Notes</h3> <p>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 (<code>SCHED_FIFO</code> 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, <code>yield</code> has no effect).</p>
<p>The POSIX equivalent of this function is <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_yield.html"><code>sched_yield</code></a>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <time.h>
#include <threads.h>
// utility function: difference between timespecs in microseconds
double usdiff(struct timespec s, struct timespec e)
{
double sdiff = difftime(e.tv_sec, s.tv_sec);
long nsdiff = e.tv_nsec - s.tv_nsec;
if(nsdiff < 0) return 1000000*(sdiff-1) + (1000000000L+nsdiff)/1000.0;
else return 1000000*(sdiff) + nsdiff/1000.0;
}
// busy wait while yielding
void sleep_100us()
{
struct timespec start, end;
timespec_get(&start, TIME_UTC);
do {
thrd_yield();
timespec_get(&end, TIME_UTC);
} while(usdiff(start, end) < 100.0);
}
int main()
{
struct timespec start, end;
timespec_get(&start, TIME_UTC);
sleep_100us();
timespec_get(&end, TIME_UTC);
printf("Waited for %.3f us\n", usdiff(start, end));
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Waited for 100.344 us</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.26.5.8 The thrd_yield function (p: 281) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.26.5.8 The thrd_yield function (p: 385) </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="thrd_sleep" title="c/thread/thrd sleep"> <span class="t-lines"><span>thrd_sleep</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> suspends execution of the calling thread for the given period of time <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/thread/yield" title="cpp/thread/yield">C++ documentation</a></span> for <code>yield</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/thread/thrd_yield" class="_attribution-link">https://en.cppreference.com/w/c/thread/thrd_yield</a>
</p>
</div>
|