{"id":8,"date":"2023-12-19T17:23:55","date_gmt":"2023-12-19T09:23:55","guid":{"rendered":"http:\/\/www.lemi7.com\/?p=8"},"modified":"2023-12-21T19:09:22","modified_gmt":"2023-12-21T11:09:22","slug":"stdthis_threadsleep_for","status":"publish","type":"post","link":"http:\/\/www.lemi7.com\/index.php\/2023\/12\/19\/stdthis_threadsleep_for\/","title":{"rendered":"std::this_thread::sleep_for"},"content":{"rendered":"\n<p>std::this_thread::sleep_for<\/p>\n\n\n\n<p>Defined in header &#8211; \u5b9a\u4e49\u4e8e\u5934\u6587\u4ef6<\/p>\n\n\n\n<ol>\n<li>std::this_thread::sleep_for<\/li>\n<\/ol>\n\n\n\n<p>template<br>void sleep_for (const chrono::duration&amp; rel_time);<\/p>\n\n\n\n<p>\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u6267\u884c\uff0c\u81f3\u5c11\u7ecf\u8fc7\u6307\u5b9a\u7684 sleep_duration\u3002<\/p>\n\n\n\n<p>\u6b64\u51fd\u6570\u53ef\u80fd\u963b\u585e\u957f\u4e8e sleep_duration\uff0c\u56e0\u4e3a\u8c03\u5ea6\u6216\u8d44\u6e90\u4e89\u8bae\u5ef6\u8fdf\u3002<\/p>\n\n\n\n<p>\u6807\u51c6\u5e93\u5efa\u8bae\u7528\u7a33\u5b9a\u65f6\u949f\u5ea6\u91cf\u65f6\u957f\u3002\u82e5\u5b9e\u73b0\u7528\u7cfb\u7edf\u65f6\u95f4\u4ee3\u66ff\uff0c\u5219\u7b49\u5f85\u65f6\u95f4\u4ea6\u53ef\u80fd\u5bf9\u65f6\u949f\u8c03\u8282\u654f\u611f\u3002<\/p>\n\n\n\n<p>Sleep for time span &#8211; \u7761\u7720\u4e00\u6bb5\u65f6\u95f4<\/p>\n\n\n\n<p>Blocks execution of the calling thread during the span of time specified by rel_time.<br>\u5728 rel_time \u6307\u5b9a\u7684\u65f6\u95f4\u6bb5\u5185\u963b\u6b62\u8c03\u7528\u7ebf\u7a0b\u7684\u6267\u884c\u3002<\/p>\n\n\n\n<p>The execution of the current thread is stopped until at least rel_time has passed from now. Other threads continue their execution.<br>\u5f53\u524d\u7ebf\u7a0b\u7684\u6267\u884c\u5c06\u505c\u6b62\uff0c\u76f4\u5230\u4ece\u73b0\u5728\u8d77\u81f3\u5c11\u7ecf\u8fc7 rel_time \u4e3a\u6b62\u3002\u5176\u4ed6\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<ol start=\"2\">\n<li>Parameters<\/li>\n<\/ol>\n\n\n\n<p>rel_time<br>The time span after which the calling thread shall resume its execution.<br>\u8c03\u7528\u7ebf\u7a0b\u5e94\u5728\u5176\u540e\u6062\u590d\u6267\u884c\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n\n\n\n<p>Note that multi-threading management operations may cause certain delays beyond this.<br>\u8bf7\u6ce8\u610f\uff0c\u591a\u7ebf\u7a0b\u7ba1\u7406\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u67d0\u4e9b\u5ef6\u8fdf\u3002<\/p>\n\n\n\n<p>duration is an object that represents a specific relative time.<br>\u6301\u7eed\u65f6\u95f4\u662f\u4ee3\u8868\u7279\u5b9a\u76f8\u5bf9\u65f6\u95f4\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u8981\u7761\u7720\u7684\u65f6\u957f\u3002<\/p>\n\n\n\n<p>resume [r\u026a&#8217;zju\u02d0m]\uff1an. \u7b80\u5386 v. \u7ee7\u7eed\uff0c\u91cd\u8fd4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1<\/code><\/pre>\n\n\n\n<ol start=\"3\">\n<li>Return value<\/li>\n<\/ol>\n\n\n\n<p>none<\/p>\n\n\n\n<ol start=\"4\">\n<li>Examples<br>4.1 std::this_thread::sleep_for<\/li>\n<\/ol>\n\n\n\n<p>\/\/============================================================================<br>\/\/ Name : std::this_thread::sleep_for<br>\/\/ Author : Yongqiang Cheng<br>\/\/ Version : Version 1.0.0<br>\/\/ Copyright : Copyright (c) 2019 Yongqiang Cheng<br>\/\/ Description : Hello World in C++, Ansi-style<br>\/\/============================================================================<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ std::cout, std::endl<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ std::this_thread::sleep_for<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ std::chrono::seconds<\/h1>\n\n\n\n<p>int main()<br>{<br>std::cout &lt;&lt; &#8220;countdown:\\n&#8221;; for (int i = 10; i &gt; 0; &#8211;i)<br>{<br>std::cout &lt;&lt; i &lt;&lt; std::endl;<br>std::this_thread::sleep_for(std::chrono::seconds(1));<br>}<br>std::cout &lt;&lt; &#8220;Lift off!\\n&#8221;;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return 0;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25<\/code><\/pre>\n\n\n\n<p>Output (after 10 seconds):<\/p>\n\n\n\n<p>countdown:<br>10<br>9<br>8<br>7<br>6<br>5<br>4<br>3<br>2<br>1<br>Lift off!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13<\/code><\/pre>\n\n\n\n<p>4.2 std::this_thread::sleep_for<\/p>\n\n\n\n<p>\/\/============================================================================<br>\/\/ Name : std::this_thread::sleep_for<br>\/\/ Author : Yongqiang Cheng<br>\/\/ Version : Version 1.0.0<br>\/\/ Copyright : Copyright (c) 2019 Yongqiang Cheng<br>\/\/ Description : Hello World in C++, Ansi-style<br>\/\/============================================================================<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ std::cout, std::endl<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ std::this_thread::sleep_for<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include \/\/ std::chrono::seconds<\/h1>\n\n\n\n<p>int main()<br>{<br>using namespace std::chrono_literals;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::cout &lt;&lt; \"Hello waiter\\n\" &lt;&lt; std::flush;\n\n\/\/ C++14\nauto start = std::chrono::high_resolution_clock::now();\nstd::this_thread::sleep_for(2s);\nauto end = std::chrono::high_resolution_clock::now();\n\nstd::chrono::duration&lt;double, std::milli&gt; elapsed = end - start;\nstd::cout &lt;&lt; \"Waited \" &lt;&lt; elapsed.count() &lt;&lt; \" ms\\n\";\n\nreturn 0;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29<\/code><\/pre>\n\n\n\n<p>10:22:39 **** Incremental Build of configuration Debug for project hello_world ****<br>make all<br>Building file: ..\/src\/hello_world.cpp<br>Invoking: GCC C++ Compiler<br>g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF&#8221;src\/hello_world.d&#8221; -MT&#8221;src\/hello_world.o&#8221; -o &#8220;src\/hello_world.o&#8221; &#8220;..\/src\/hello_world.cpp&#8221;<br>Finished building: ..\/src\/hello_world.cpp<\/p>\n\n\n\n<p>Building target: hello_world<br>Invoking: GCC C++ Linker<br>g++ -o &#8220;hello_world&#8221; .\/src\/hello_world.o -lpthread<br>Finished building target: hello_world<\/p>\n\n\n\n<p>10:22:40 Build Finished (took 895ms)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14<\/code><\/pre>\n\n\n\n<p>Hello waiter<br>Waited 2000.06 ms<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3<\/code><\/pre>\n\n\n\n<ol start=\"7\">\n<li>Exception safety &#8211; \u5f02\u5e38\u5b89\u5168\u6027<\/li>\n<\/ol>\n\n\n\n<p>If the type of rel_time never throws exceptions (like the instantiations of duration in header ), this function never throws exceptions (no-throw guarantee).<br>\u5982\u679c rel_time \u7684\u7c7b\u578b\u4ece\u4e0d\u629b\u51fa\u5f02\u5e38 (\u5982\u5934\u6587\u4ef6 \u4e2d\u7684 duration` \u5b9e\u4f8b\u5316)\uff0c\u5219\u6b64\u51fd\u6570\u4ece\u4e0d\u629b\u51fa\u5f02\u5e38 (\u65e0\u629b\u51fa\u4fdd\u8bc1)\u3002<\/p>\n\n\n\n<p>assignment [\u0259&#8217;sa\u026anm\u0259nt]\uff1an. \u4efb\u52a1\uff0c\u5e03\u7f6e\uff0c\u8d4b\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1<\/code><\/pre>\n\n\n\n<p>References<\/p>\n\n\n\n<p>http:\/\/www.cplusplus.com\/reference\/thread\/this_thread\/sleep_for\/<br>https:\/\/en.cppreference.com\/w\/cpp\/thread\/sleep_for<br><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>std::this_thread::sl<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts\/8"}],"collection":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/comments?post=8"}],"version-history":[{"count":1,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":9,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/posts\/8\/revisions\/9"}],"wp:attachment":[{"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.lemi7.com\/index.php\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}