{"id":5270,"date":"2017-06-02T04:50:41","date_gmt":"2017-06-02T04:50:41","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/blocking-and-non-blocking\/"},"modified":"2023-06-26T12:15:17","modified_gmt":"2023-06-26T12:15:17","slug":"blocking-and-non-blocking","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/blocking-and-non-blocking\/","title":{"rendered":"Blocking and Non-Blocking Algorithms"},"content":{"rendered":"<p>Blocking, non-blocking, lock-free, and wait-free. Each of these terms describes a key characteristic of an algorithm when executed in a concurrent environment. So, reasoning about the runtime behavior of your program often means putting your algorithm in the right bucket. Therefore, this post is about buckets.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>An algorithm fall into one of two buckets: blocking or non-blocking.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5267\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/BlockingNonBlocking.png\" alt=\"BlockingNonBlocking\" width=\"300\" height=\"223\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/BlockingNonBlocking.png 796w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/BlockingNonBlocking-300x223.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/BlockingNonBlocking-768x572.png 768w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Let&#8217;s first talk about blocking.<\/p>\n<h2>Blocking<\/h2>\n<p>Intuitively, it is quite clear what blocking for an algorithm mean. But concurrency is not about intuition, it&#8217;s about precise terms. The easiest way to define blocking is to define it with the help of non-blocking.<\/p>\n<ul>\n<li><strong>Non-blocking: <\/strong>An algorithm is called non-blocking if the failure or suspension of any thread cannot cause the failure or suspension of another thread. (<a href=\"http:\/\/jcip.net\/\">Java concurrency in practice<\/a>)<\/li>\n<\/ul>\n<p>There is not any word about locking in this definition. That&#8217;s right. Non-blocking is a broader term.<\/p>\n<p>To block a program is relatively easy. The typical use case is to use more than one mutex and lock them in a different sequence. Nice timing, and you have a deadlock. But there are a lot more ways to produce blocking behavior.<\/p>\n<p>Each time you have to wait for a resource, a block is possible.<\/p>\n<p>Here are a few examples for synchronizing access to a resource:<\/p>\n<ul>\n<li>A condition variable with <span style=\"font-family: Courier New,Courier,monospace;\">wait<\/span>.<\/li>\n<li>A future with <span style=\"font-family: Courier New,Courier,monospace;\">wait <\/span>or <span style=\"font-family: Courier New,Courier,monospace;\">get<\/span>.<\/li>\n<\/ul>\n<p>Even the join call of a thread can be used to block a thread.<\/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;\">\/\/ deadlockWait.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;mutex&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>mutex coutMutex;\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n  std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span> t([]{\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Still waiting ...\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;            <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>lock_guard<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lockGuard(coutMutex);         <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"child: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>get_id() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;}\r\n  );\r\n\r\n  {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>lock_guard<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span>mutex<span style=\"color: #555555;\">&gt;<\/span> lockGuard(coutMutex);          <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"creator: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>this_thread<span style=\"color: #555555;\">::<\/span>get_id() <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    t.join();                                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 5<\/span>\r\n\r\n  }                                                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ 4<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The program run will block immediately.&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5268\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/deadlockWait.png\" alt=\"deadlockWait\" width=\"400\" height=\"152\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/deadlockWait.png 643w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/deadlockWait-300x114.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>What is happening?&nbsp;The creator thread locks in (1) the mutex. Now, the child thread executes (2). To get the mutex in expression (3), the creator thread has at first to unlock it. But the creator thread will only unlock the mutex if the lockGuard (1) goes in (4) out of scope. That&nbsp;will never happen because the child thread has at first to lock the mutex <span style=\"font-family: courier new,courier;\">coutMutex<\/span>.<\/p>\n<p>Let&#8217;s have a look at the non-blocking algorithms.<\/p>\n<\/p>\n<h2>Non-blocking<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5269\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/NonBlocking.png\" alt=\"NonBlocking\" width=\"400\" height=\"388\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/NonBlocking.png 613w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/06\/NonBlocking-300x291.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>The main categories for non-blocking algorithms are lock-free and wait-free. Each wait-free algorithm is lock-free, and each lock-free is non-blocking. Non-blocking and lock-free are not the same. There is an additional guarantee called obstruction-free, which I will ignore in this post because it is not so relevant.<\/p>\n<p>Non-blocking algorithms are typically implemented with CAS instructions. CAS stands for compare and swap. CAS is called <span style=\"font-family: courier new,courier;\">compare_exchange_strong<\/span> or <span style=\"font-family: courier new,courier;\">compare_exchange_weak<\/span> in C++.<\/p>\n<p>I will in this post only refer to the strong version. For more information, read my previous post <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-atomic-boolean\">The Atomic Boolean<\/a>. The key idea of both operations is a call of&nbsp;<span style=\"font-family: courier new,courier;\">atomicValue.compare_exchange_strong(expected, desired<\/span>) obeys the following rules in an atomic fashion.<\/p>\n<ul>\n<li>If the atomic comparison of&nbsp;<span style=\"font-family: courier new,courier;\">atomicValue<\/span> with expected returns&nbsp;<span style=\"font-family: courier new,courier;\">true<\/span>,<span style=\"font-family: courier new,courier;\">&nbsp;atomicValue<\/span> will be set in the same atomic operation as&nbsp;<span style=\"font-family: courier new,courier;\">desired<\/span>.<\/li>\n<li>If the comparison returns&nbsp;<span style=\"font-family: courier new,courier;\">false<\/span>, <span style=\"font-family: courier new,courier;\">expected<\/span> will be set to&nbsp;<span style=\"font-family: courier new,courier;\">atomicValue<\/span>.<\/li>\n<\/ul>\n<p>Let&#8217;s now have a closer look at lock-free versus wait-free.&nbsp;<\/p>\n<p>At first, the definition of lock-free and wait-free. Both definitions are quite similar. Therefore, it makes a lot of sense to define them together.<\/p>\n<ul>\n<li><strong>Lock-free:<\/strong> A non-blocking algorithm is lock-free if there is guaranteed system-wide progress.<\/li>\n<li><strong>Wait-free:<\/strong> A non-blocking algorithm is wait-free if there is guaranteed per-thread progress.<\/li>\n<\/ul>\n<h3>Lock-free<\/h3>\n<p>&nbsp;<\/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;\">\/\/ fetch_mult.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;atomic&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\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> T<span style=\"color: #555555;\">&gt;<\/span>\r\nT fetch_mult(std<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&amp;<\/span> shared, T mult){                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\n  T oldValue <span style=\"color: #555555;\">=<\/span> shared.load();                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">while<\/span> (<span style=\"color: #555555;\">!<\/span>shared.compare_exchange_strong(oldValue, oldValue <span style=\"color: #555555;\">*<\/span> mult));  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldValue;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n  std<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myInt{<span style=\"color: #ff6600;\">5<\/span>};\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myInt <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;          \r\n  fetch_mult(myInt,<span style=\"color: #ff6600;\">5<\/span>);\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myInt <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;         \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The algorithm<span style=\"font-family: courier new,courier;\"> fetch_mult<\/span> (1) multiplies an <span style=\"font-family: courier new,courier;\">std::atomic shared<\/span> by <span style=\"font-family: courier new,courier;\">mult<\/span>. The critical observation is that there is a small time window between the reading of the old value <span style=\"font-family: courier new,courier;\">T oldValue = shared Load<\/span> (2) and the&nbsp;comparison with the new value (3). Therefore, another thread can always kick in and change the <span style=\"font-family: courier new,courier;\">oldValue<\/span>. If you reason about such a bad interleaving of threads, you see that there can be no&nbsp;per-thread progress guarantee.&nbsp;<\/p>\n<p>Therefore, the algorithm is lock-free but not wait-free.<\/p>\n<p>Here is the output of the program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4792\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/06\/fetch_mult.PNG\" alt=\"fetch mult\" width=\"500\" height=\"84\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/06\/fetch_mult.PNG 1148w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/06\/fetch_mult-300x50.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/06\/fetch_mult-1024x171.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/06\/fetch_mult-768x128.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>While a lock-free algorithm guarantees system-wide progress, a wait-free algorithm guarantees per-thread progress.<\/p>\n<h3>Wait-free<\/h3>\n<p>You will see if you reason about the lock-free algorithm in the last example. A<span style=\"font-family: courier new,courier;\"> compare_exchange_strong<\/span> call involves synchronization. First, you read the old value, and then update the new value if the initial condition already holds. If the initial condition holds, you publish the new value. If not, you&nbsp;do it again if you put the call in a while loop. Therefore <span style=\"font-family: courier new,courier;\">compare_exchange_strong<\/span> behaves like an atomic transaction.<\/p>\n<p>The crucial part of the following program needs no synchronization.<\/p>\n<p>&nbsp;<\/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;\">\/\/ relaxed.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;vector&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;thread&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;atomic&gt;<\/span>\r\n \r\nstd<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> cnt <span style=\"color: #555555;\">=<\/span> {<span style=\"color: #ff6600;\">0<\/span>};\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">add<\/span>(){                                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; n <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">1000<\/span>; <span style=\"color: #555555;\">++<\/span>n) {\r\n        cnt.fetch_add(<span style=\"color: #ff6600;\">1<\/span>, std<span style=\"color: #555555;\">::<\/span>memory_order_relaxed);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\n    }\r\n}\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>()\r\n{\r\n    std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>std<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">thread<\/span><span style=\"color: #555555;\">&gt;<\/span> v;\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> n <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; n <span style=\"color: #555555;\">&lt;<\/span> <span style=\"color: #ff6600;\">10<\/span>; <span style=\"color: #555555;\">++<\/span>n) {\r\n        v.emplace_back(add);\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span><span style=\"color: #555555;\">&amp;<\/span> t <span style=\"color: #555555;\">:<\/span> v) {\r\n        t.join();\r\n    }\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Final counter value is \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> cnt <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Have a closer look at function add (1). There is no synchronization involved in expression (2). The value 1 is just added to the atomic <span style=\"font-family: courier new,courier;\">cnt<\/span>.<\/p>\n<p>And here is the output of the program. We&nbsp;always get 100<span id=\"transmark\"><\/span>00 because 10 threads increment the value 1000 times.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4830\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/relaxed.png\" alt=\"relaxed\" width=\"400\" height=\"357\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/relaxed.png 527w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/relaxed-300x268.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>For simplicity reasons, I ignored a few other guarantees in this post, such as starvation-free as a subset of blocking or wait-free bounded as a subset of wait-free. You can read the details on the blog <a href=\"http:\/\/concurrencyfreaks.blogspot.de\/2013\/05\/lock-free-and-wait-free-definition-and.html\">Concurrency Freaks<\/a>.&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/aba-a-is-not-the-same-as-a\">next post<\/a>, I will write about curiosity. The so-called ABA problem is a kind of false-positive case for CAS instructions. That means although the old value of a CAS instruction is still the same, it changed in the meantime.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Blocking, non-blocking, lock-free, and wait-free. Each of these terms describes a key characteristic of an algorithm when executed in a concurrent environment. So, reasoning about the runtime behavior of your program often means putting your algorithm in the right bucket. Therefore, this post is about buckets.<\/p>\n","protected":false},"author":21,"featured_media":5267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[505,434,504],"class_list":["post-5270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading","tag-acquire-release-semantic","tag-atomics","tag-relaxed-semantics"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5270","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=5270"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5270\/revisions"}],"predecessor-version":[{"id":6870,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5270\/revisions\/6870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5267"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}