{"id":1867,"date":"2019-01-13T14:10:37","date_gmt":"2019-01-13T13:10:37","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=1867"},"modified":"2019-01-13T14:11:28","modified_gmt":"2019-01-13T13:11:28","slug":"c17-stdscoped_lock","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2019\/01\/13\/c17-stdscoped_lock\/","title":{"rendered":"C++17: std::scoped_lock"},"content":{"rendered":"\n<p>C++17 includes an <strong>std::scoped_lock<\/strong> (defined in &lt;mutex&gt;) which basically replaces std::lock_guard. Both are RAII classes that automatically unlock a mutex when they are destroyed. The only difference between the two is that the new scoped_lock can handle multiple mutexes automatically, while lock_guard is limited to a single mutex.<\/p>\n\n\n\n<p>Using the scoped_lock RAII class is straightforward. Here is an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstd::mutex mutex1;\n\/\/ ...\nstd::scoped_lock&lt;std::mutex&gt; sl(mutex1);\n<\/pre><\/div>\n\n\n<p>The scoped_lock class template is a variadic template supporting any number of mutexes:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstd::mutex mutex1;\nstd::recursive_mutex mutex2;\n\/\/ ...\nstd::scoped_lock&lt;std::mutex, std::recursive_mutex&gt; sl(mutex1, mutex2);\n<\/pre><\/div>\n\n\n<p>The declaration of the scoped_lock can be simplified thanks to C++17 <a href=\"http:\/\/www.nuonsoft.com\/blog\/2018\/12\/17\/c17-template-argument-deduction-for-constructors\/\">template argument deduction for constructors<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstd::mutex mutex1;\nstd::recursive_mutex mutex2;\n\/\/ ...\nstd::scoped_lock sl(mutex1, mutex2);\n<\/pre><\/div>\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++17 includes an std::scoped_lock (defined in &lt;mutex&gt;) which basically replaces std::lock_guard. Both are RAII classes that automatically unlock a mutex when they are destroyed. The only difference between the two is that the new scoped_lock can handle multiple mutexes automatically, while lock_guard is limited to a single mutex. Using the scoped_lock RAII class is straightforward. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[222,235,228],"class_list":["post-1867","post","type-post","status-publish","format-standard","hentry","category-c","tag-c17","tag-scoped_lock","tag-visual-c-2017"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1867"}],"version-history":[{"count":10,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions"}],"predecessor-version":[{"id":1878,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/1867\/revisions\/1878"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}