{"id":5890,"date":"2020-05-01T06:34:07","date_gmt":"2020-05-01T06:34:07","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-20-thread-pools-with-cppcoro\/"},"modified":"2023-09-28T07:42:02","modified_gmt":"2023-09-28T07:42:02","slug":"c-20-thread-pools-with-cppcoro","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-20-thread-pools-with-cppcoro\/","title":{"rendered":"C++20: Thread Pools with cppcoro"},"content":{"rendered":"<p>This post is the third and final post in my miniseries to <a href=\"https:\/\/github.com\/lewissbaker\/cppcoro\">cppcoro<\/a>. cppcoro is a library of coroutine abstractions from Lewis Baker. Today, I introduce thread pools.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8406 size-full\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/TimelineCpp20Coroutines.png\" alt=\"\" width=\"1068\" height=\"383\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/TimelineCpp20Coroutines.png 1068w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/TimelineCpp20Coroutines-300x108.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/TimelineCpp20Coroutines-1030x369.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/TimelineCpp20Coroutines-768x275.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2021\/02\/TimelineCpp20Coroutines-705x253.png 705w\" sizes=\"auto, (max-width: 1068px) 100vw, 1068px\" \/><\/p>\n<p>To get the most out of this post, you should know my two previous posts to cppcoro.<\/p>\n<ul>\n<li><a href=\"https:\/\/bit.ly\/CoroutinesCppcoro\">C++20: Coroutines with cppcoro<\/a>: the introduction to cppcoro and the elementary coroutines task and generator<\/li>\n<li><a href=\"https:\/\/bit.ly\/CoroutineAbstractions\">C++20: Powerful Coroutines with cppcoro<\/a>: more powerful coroutines with threads<\/li>\n<\/ul>\n<p><span style=\"font-family: courier new, courier;\">Additionally to the cppcoro::sync_wait<\/span> function, which can be used to wait until the specified <em>Awaitable<\/em> completes, cppcoro offers the quite interesting <span style=\"font-family: courier new, courier;\">cppcoro::when_all<\/span> function.<\/p>\n<h2><span style=\"font-family: courier new, courier;\">when_all<\/span><\/h2>\n<ul>\n<li><strong><span style=\"font-family: courier new, courier;\">when_all: <\/span><\/strong> creates an <em>Awaitable<\/em>, that waits for all its <em>Input-Awaitables<\/em>, and returns an aggregate of their individual results.<\/li>\n<\/ul>\n<p>I simplified the definition of the function <span style=\"font-family: courier new, courier;\">cpporo::when_all. The following<\/span>\u00a0example should help to get the first impression.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ cppcoroWhenAll.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\n\n<span style=\"color: #009999;\">#include &lt;cppcoro\/sync_wait.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/task.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/when_all.hpp&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>chrono_literals;\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> getFirst() {\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    co_return <span style=\"color: #cc3300;\">\"First\"<\/span>;\n}\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> getSecond() {\n     std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    co_return <span style=\"color: #cc3300;\">\"Second\"<\/span>;\n}\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> getThird() {\n     std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    co_return <span style=\"color: #cc3300;\">\"Third\"<\/span>;\n}\n\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;&gt;<\/span> runAll() {\n                                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span>[fir, sec, thi] <span style=\"color: #555555;\">=<\/span> co_await cppcoro<span style=\"color: #555555;\">::<\/span>when_all(getFirst(), getSecond(), getThird());\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> fir <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sec <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> thi <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\n    \n    cppcoro<span style=\"color: #555555;\">::<\/span>sync_wait(runAll());                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> end <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>high_resolution_clock<span style=\"color: #555555;\">::<\/span>now();\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> elapsed <span style=\"color: #555555;\">=<\/span> end <span style=\"color: #555555;\">-<\/span> start;   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Execution time \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> elapsed.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" seconds.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n}\n<\/pre>\n<\/div>\n<p>The top-level task <span style=\"font-family: courier new, courier;\">cppcoro::sync_wait(runAll())<\/span> (line 1) awaits the Awaitable <span style=\"font-family: courier new, courier;\">runAll. runAll<\/span> awaits the Awaitables <span style=\"font-family: courier new, courier;\">getFirst<\/span>, <span style=\"font-family: courier new, courier;\">getSecond<\/span>, and <span style=\"font-family: courier new, courier;\">getThird<\/span> (line 2). The Awaitables <span style=\"font-family: courier new, courier;\">runAll<\/span>, <span style=\"font-family: courier new, courier;\">getFirst<\/span>, <span style=\"font-family: courier new, courier;\">getSecond<\/span>, and <span style=\"font-family: courier new, courier;\">getThird<\/span> are coroutines. Each of the get functions sleeps for one second (line 3). Three times one second makes three seconds. This is when the call <span style=\"font-family: courier new, courier;\">cppcoro::sync_wait(runAll()) <\/span>waits for the coroutines. Line 4 displays the time duration.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5887\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroWhenAll.png\" alt=\"cppcoroWhenAll\" width=\"350\" height=\"197\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroWhenAll.png 454w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroWhenAll-300x169.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>Now that you get the basics of the function cppcoro::when_all, let me add a thread pool.<\/p>\n<h2><span style=\"font-family: courier new, courier;\">static_thread_pool<\/span><\/h2>\n<ul>\n<li><span style=\"font-family: courier new, courier;\"><strong>static_thead_pool<\/strong><\/span>: schedule work on a fixed-size pool of threads<\/li>\n<\/ul>\n<p><span style=\"font-family: courier new, courier;\">cppcoro::static_thread_pool<\/span> can be invoked with and without a number. The number stands for the number of threads that are created. If you don&#8217;t specify a number, the C++11 function <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/thread\/thread\/hardware_concurrency\"><span style=\"font-family: courier new, courier;\">std::thread::hardware_concurrency()<\/span><\/a> is used. <span style=\"font-family: courier new, courier;\">std::thread::hardware_concurrency<\/span> hints at the number of hardware threads supported by your system. This may be the number of processors or cores you have.<\/p>\n<p>Let me try it out. Based on the previous one, the following example executes the coroutines <span style=\"font-family: courier new, courier;\">getFirst<\/span>, <span style=\"font-family: courier new, courier;\">getSecond<\/span>, and <span style=\"font-family: courier new, courier;\">getThird<\/span> concurrently.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ cppcoroWhenAllOnThreadPool.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\n\n<span style=\"color: #009999;\">#include &lt;cppcoro\/sync_wait.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/task.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/static_thread_pool.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/when_all.hpp&gt;<\/span>\n\n\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>chrono_literals;\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> getFirst() {\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);\n    co_return <span style=\"color: #cc3300;\">\"First\"<\/span>;\n}\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> getSecond() {\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);\n    co_return <span style=\"color: #cc3300;\">\"Second\"<\/span>;\n}\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> getThird() {\n    std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);\n    co_return <span style=\"color: #cc3300;\">\"Third\"<\/span>;\n}\n\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Func<span style=\"color: #555555;\">&gt;<\/span>\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&gt;<\/span> runOnThreadPool(cppcoro<span style=\"color: #555555;\">::<\/span>static_thread_pool<span style=\"color: #555555;\">&amp;<\/span> tp, Func func) {\n    co_await tp.schedule();\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> res <span style=\"color: #555555;\">=<\/span> co_await func();\n    co_return res;\n}\n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;&gt;<\/span> runAll(cppcoro<span style=\"color: #555555;\">::<\/span>static_thread_pool<span style=\"color: #555555;\">&amp;<\/span> tp) {\n    \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span>[fir, sec, thi] <span style=\"color: #555555;\">=<\/span> co_await cppcoro<span style=\"color: #555555;\">::<\/span>when_all(    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n        runOnThreadPool(tp, getFirst),\n        runOnThreadPool(tp, getSecond), \n        runOnThreadPool(tp, getThird));\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> fir <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sec <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> thi <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n}\n    \n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\n\n    cppcoro<span style=\"color: #555555;\">::<\/span>static_thread_pool tp;                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n    cppcoro<span style=\"color: #555555;\">::<\/span>sync_wait(runAll(tp));                         <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> end <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>high_resolution_clock<span style=\"color: #555555;\">::<\/span>now();\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> elapsed <span style=\"color: #555555;\">=<\/span> end <span style=\"color: #555555;\">-<\/span> start;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Execution time \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> elapsed.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" seconds.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n}\n<\/pre>\n<\/div>\n<p>Here are the crucial differences to the previous program <span style=\"font-family: courier new, courier;\">cppcoroWhenAll.cpp<\/span>. I created in line (1) a thread pool <span style=\"font-family: courier new, courier;\">tp<\/span> and used it as an argument for the function <span style=\"font-family: courier new, courier;\">runAll(tp)<\/span> (line 2). The function runAll uses the thread pool to start the coroutines concurrently. Thanks to structured binding (line 3), the values of each coroutine can be easily aggregated and assigned to a variable. In the end, the main function takes one instead of three seconds.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5888\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroWhenAllOnThreadPool.png\" alt=\"cppcoroWhenAllOnThreadPool\" width=\"450\" height=\"212\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroWhenAllOnThreadPool.png 544w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroWhenAllOnThreadPool-300x141.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>Maybe you know that we get with C++20 latches and barriers. Latches and barriers are thread synchronization mechanisms that enable some threads to block until a counter becomes zero. cppcoro also supports latches and barriers.<\/p>\n<h2><span style=\"font-family: courier new, courier;\">async_latch<\/span><\/h2>\n<ul>\n<li><strong>async_latch<\/strong>: allows coroutines to wait until a counter becomes zero asynchronously<\/li>\n<\/ul>\n<p>The following program <span style=\"font-family: courier new, courier;\">cppcoroLatch.cpp<\/span> shows thread synchronization with a <span style=\"font-family: courier new, courier;\">cppcoro::async_latch<\/span>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ cppcoroLatch.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;future&gt;<\/span>\n\n<span style=\"color: #009999;\">#include &lt;cppcoro\/sync_wait.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/async_latch.hpp&gt;<\/span>\n<span style=\"color: #009999;\">#include &lt;cppcoro\/task.hpp&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std<span style=\"color: #555555;\">::<\/span>chrono_literals; \n\ncppcoro<span style=\"color: #555555;\">::<\/span>task<span style=\"color: #555555;\">&lt;&gt;<\/span> waitFor(cppcoro<span style=\"color: #555555;\">::<\/span>async_latch<span style=\"color: #555555;\">&amp;<\/span> latch) {\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Before co_await\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n    co_await latch;                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"After co_await\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main() {\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n    cppcoro<span style=\"color: #555555;\">::<\/span>async_latch latch(<span style=\"color: #ff6600;\">3<\/span>);              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)\n\n                                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span><\/span>\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> waiter <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>async([<span style=\"color: #555555;\">&amp;<\/span>latch]{ cppcoro<span style=\"color: #555555;\">::<\/span>sync_wait(waitFor(latch)); }); \n\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> counter1 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>async([<span style=\"color: #555555;\">&amp;<\/span>latch] {       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n        std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">2<\/span>s);\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"counter1: latch.count_down() \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n        latch.count_down();\n    });\n        \n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> counter2 <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>async([<span style=\"color: #555555;\">&amp;<\/span>latch] {      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\n        std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>sleep_for(<span style=\"color: #ff6600;\">1<\/span>s);\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"counter2: latch.count_down(2) \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n        latch.count_down(<span style=\"color: #ff6600;\">2<\/span>);\n    });\n\n    waiter.get(), counter1.get(), counter2.get();\n    \n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\n\n}\n<\/pre>\n<\/div>\n<p>I create the cppcoro::asynch_latch in line (1) and initialize the counter to 3. This time, I use <span style=\"font-family: courier new, courier;\">std::async<\/span> (line 2) to run the three coroutines concurrently. Each <span style=\"font-family: courier new, courier;\">std::async<\/span> call gets the <span style=\"font-family: courier new, courier;\">latch<\/span> per reference. The <span style=\"font-family: courier new, courier;\">waitFor<\/span> coroutine waits in line 3 until the counter becomes zero. The coroutine <span style=\"font-family: courier new, courier;\">counter1<\/span> sleeps for 2 seconds before it counts down by 1. In contrast, the <span style=\"font-family: courier new, courier;\">counter2<\/span> sleeps for 1 second and counts down by 2. The screenshot shows the interleaving of the threads.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5889\" style=\"display: block; margin-left: auto; margin-right: auto;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroLatch.png\" alt=\"cppcoroLatch\" width=\"450\" height=\"251\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroLatch.png 485w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/05\/cppcoroLatch-300x167.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>So far, I have written about three of the <a href=\"https:\/\/www.modernescpp.com\/index.php\/thebigfour\">big four of C++20<\/a>: concepts, ranges, and coroutines. Modules are still missing in my tour through the big four and are the topic of my <a href=\"https:\/\/www.modernescpp.com\/index.php\/cpp20-modules\">next posts.<\/a><\/p>\n<p>By the way, if anyone wants to write a post on a C++20 feature I will write about, please get in touch with me. I&#8217;m happy to publish and translate it into English\/German if necessary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is the third and final post in my miniseries to cppcoro. cppcoro is a library of coroutine abstractions from Lewis Baker. Today, I introduce thread pools.<\/p>\n","protected":false},"author":21,"featured_media":8406,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[445],"class_list":["post-5890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-coroutines"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5890"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5890\/revisions"}],"predecessor-version":[{"id":8417,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5890\/revisions\/8417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/8406"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}