{"id":7861,"date":"2020-07-24T12:15:35","date_gmt":"2020-07-24T12:15:35","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=7861"},"modified":"2024-02-16T10:24:17","modified_gmt":"2024-02-16T10:24:17","slug":"stdrecursive_mutex","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=7861","title":{"rendered":"std::recursive_mutex (C++)"},"content":{"rendered":"<p>The std::mutex is a non-recursive mutex and therefore can&#8217;t be locked again from the same thread before getting unlocked. This can lead quite fast to a deadlock. That&#8217;s the point where std::recursive_mutex comes in place. It can be locked multiple time from the same threat without the need to unlock it first.<\/p>\n<hr>\n<p><u>Example &#8211; With deadlock<\/u><\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n#include &lt;thread&gt;\r\n#include &lt;mutex&gt;\r\n\r\nclass Cache\r\n{\r\npublic:\r\n  Cache() : m_Mtx(), m_Val(0) {}\r\n\r\n  bool update(int val) {\r\n    std::lock_guard&lt;std::mutex&gt; lock(m_Mtx);\r\n\r\n    bool updated = false;\r\n    if (m_Val != val) {\r\n      write(val);\r\n      updated = true;\r\n      std::cout &lt;&lt; &quot;val has been updated&quot; &lt;&lt; std::endl;\r\n    }\r\n    return updated;\r\n  }\r\n\r\nprivate:\r\n  void write(int val) {\r\n    std::lock_guard&lt;std::mutex&gt; lock(m_Mtx); \/\/ 2nd lock --&gt; deadlock\r\n    m_Val = val;\r\n  }\r\n\r\n  std::mutex m_Mtx;\r\n  int m_Val;\r\n};\r\n\r\nint main()\r\n{\r\n  Cache cache;\r\n  std::thread t(&amp;Cache::update, &amp;cache, 42);\r\n  t.join();\r\n\r\n  return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The std::mutex is a non-recursive mutex and therefore can&#8217;t be locked again from the same thread before getting unlocked. This<\/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\/7861"}],"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=7861"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7861\/revisions"}],"predecessor-version":[{"id":16748,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/7861\/revisions\/16748"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7861"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}