summaryrefslogtreecommitdiff
path: root/devdocs/c/thread%2Fthrd_sleep.html
blob: 2c7cd59ae469fa1b1d2bc7359cf8f9253ad0f138 (plain)
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
    <h1 id="firstHeading" class="firstHeading">thrd_sleep</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">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 &lt;threads.h&gt;
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
 
int main(void)
{
    printf("Time: %s", ctime(&amp;(time_t){time(NULL)}));
    thrd_sleep(&amp;(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
    printf("Time: %s", ctime(&amp;(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">
    &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_sleep" class="_attribution-link">https://en.cppreference.com/w/c/thread/thrd_sleep</a>
  </p>
</div>