diff options
Diffstat (limited to 'devdocs/c/thread%2Fthrd_sleep.html')
| -rw-r--r-- | devdocs/c/thread%2Fthrd_sleep.html | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/devdocs/c/thread%2Fthrd_sleep.html b/devdocs/c/thread%2Fthrd_sleep.html new file mode 100644 index 00000000..2c7cd59a --- /dev/null +++ b/devdocs/c/thread%2Fthrd_sleep.html @@ -0,0 +1,36 @@ + <h1 id="firstHeading" class="firstHeading">thrd_sleep</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">int thrd_sleep( const struct timespec* duration, + struct timespec* remaining );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Blocks the execution of the current thread for <i>at least</i> until the <code>TIME_UTC</code> based duration pointed to by <code>duration</code> has elapsed.</p> +<p>The sleep may resume earlier if a <code><a href="../program/signal" title="c/program/signal">signal</a></code> that is not ignored is received. In such case, if <code>remaining</code> is not <code><a href="../types/null" title="c/types/NULL">NULL</a></code>, the remaining time duration is stored into the object pointed to by <code>remaining</code>.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> duration </td> <td> - </td> <td> pointer to the duration to sleep for </td> +</tr> <tr class="t-par"> <td> remaining </td> <td> - </td> <td> pointer to the object to put the remaining time on interruption. May be <code><a href="../types/null" title="c/types/NULL">NULL</a></code>, in which case it is ignored </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p><code>0</code> on successful sleep, <code>-1</code> if a signal occurred, other negative value if an error occurred.</p> +<h3 id="Notes"> Notes</h3> <p><code>duration</code> and <code>remaining</code> may point at the same object, which simplifies re-running the function after a signal.</p> +<p>The actual sleep time may be longer than requested because it is rounded up to the timer granularity and because of scheduling and context switching overhead.</p> +<p>The POSIX equivalent of this function is <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html"><code>nanosleep</code></a>.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <threads.h> +#include <time.h> +#include <stdio.h> + +int main(void) +{ + printf("Time: %s", ctime(&(time_t){time(NULL)})); + thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec + printf("Time: %s", ctime(&(time_t){time(NULL)})); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">Time: Mon Feb 2 16:18:41 2015 +Time: Mon Feb 2 16:18:42 2015</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.26.5.7 The thrd_sleep function (p: 281) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.26.5.7 The thrd_sleep 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_yield" title="c/thread/thrd yield"> <span class="t-lines"><span>thrd_yield</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> yields the current time slice <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/sleep_for" title="cpp/thread/sleep for">C++ documentation</a></span> for <code>sleep_for</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_sleep" class="_attribution-link">https://en.cppreference.com/w/c/thread/thrd_sleep</a> + </p> +</div> |
