{"id":11008,"date":"2021-04-19T09:19:16","date_gmt":"2021-04-19T09:19:16","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=11008"},"modified":"2025-08-14T15:51:57","modified_gmt":"2025-08-14T15:51:57","slug":"active-vs-passive-waiting-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=11008","title":{"rendered":"Active vs. passive waiting (C++)"},"content":{"rendered":"<h2>Active waiting<\/h2>\n<p>Don&#8217;t use this approach, because it will unnecessarily consume CPU time.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nwhile (!m_flag) \/\/ std::atomic&lt;bool&gt;\r\n{\r\n  std::this_thread::sleep_for(std::chrono::milliseconds(100));\r\n}\r\n\/\/ do stuff\r\n<\/pre>\n<p>The sleep reduces the CPU consumption, but increases the response time.<\/p>\n<p>More:<br \/>\n&#8211; There is also std::this_thread::yield(), but it spins very often and consumes a lot of CPU.<\/p>\n<hr>\n<h2>Passive waiting<\/h2>\n<p>Use this approach, because it won&#8217;t unnecessarily consume CPU time.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nstd::unique_lock lock(cv_mutex_);\r\nwhile (!m_flag) {\r\n    m_condVar.wait(lock);\r\n}\r\n\/\/ do stuff\r\n<\/pre>\n<p>Alternative:<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nstd::unique_lock lock(cv_mutex_);\r\ncv_.wait(lock, [] { return m_flag; });\r\n\/\/ do stuff\r\n<\/pre>\n<p>The mutex gets released as long as the cv is passively waiting for a notification.<\/p>\n<p>More:<br \/>\n&#8211; It&#8217;s also possible that wait returns even if the cv has no notify yet, it&#8217;s called spurious wakeup.<br \/>\n&#8211; In the following post, you find an example: <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=19024\">Singleton file logger with concurrent_queue (C++)<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Active waiting Don&#8217;t use this approach, because it will unnecessarily consume CPU time. The sleep reduces the CPU consumption, but<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[63],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11008"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11008"}],"version-history":[{"count":7,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11008\/revisions"}],"predecessor-version":[{"id":19030,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11008\/revisions\/19030"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11008"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}