summaryrefslogtreecommitdiff
path: root/devdocs/c/thread%2Fthrd_yield.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/thread%2Fthrd_yield.html')
-rw-r--r--devdocs/c/thread%2Fthrd_yield.html52
1 files changed, 52 insertions, 0 deletions
diff --git a/devdocs/c/thread%2Fthrd_yield.html b/devdocs/c/thread%2Fthrd_yield.html
new file mode 100644
index 00000000..7eb8bcad
--- /dev/null
+++ b/devdocs/c/thread%2Fthrd_yield.html
@@ -0,0 +1,52 @@
+ <h1 id="firstHeading" class="firstHeading">thrd_yield</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;threads.h&gt;</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 &lt;stdio.h&gt;
+#include &lt;time.h&gt;
+#include &lt;threads.h&gt;
+
+// 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 &lt; 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(&amp;start, TIME_UTC);
+ do {
+ thrd_yield();
+ timespec_get(&amp;end, TIME_UTC);
+ } while(usdiff(start, end) &lt; 100.0);
+}
+
+int main()
+{
+ struct timespec start, end;
+ timespec_get(&amp;start, TIME_UTC);
+ sleep_100us();
+ timespec_get(&amp;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">
+ &copy; 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>