{"id":11978,"date":"2021-08-20T12:56:12","date_gmt":"2021-08-20T12:56:12","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=11978"},"modified":"2024-02-16T10:23:03","modified_gmt":"2024-02-16T10:23:03","slug":"types-of-mutexes-and-locks-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=11978","title":{"rendered":"Types of mutexes and locks (C++)"},"content":{"rendered":"<p><strong>Mutexes<\/strong> (Selection)<br \/>\n&#8230; synchronizes the access on critical sections<\/p>\n<table>\n<col width=\"25%\">\n<col width=\"75%\">\n<tr>\n<td>std::mutex<\/td>\n<td>Provides exclusive access (can only be pulled by one thread at a time and only once in that time)<\/td>\n<tr>\n<td>std::recursive_mutex<\/td>\n<td>Provides exclusive access (can only be pulled by one thread at a time but infinitely often in that time)<\/td>\n<tr>\n<td>std::shared_mutex<\/td>\n<td>Provides exclusive access (if pulled by a thread no other exclusive and no shared access possible) and shared access (if pulled by atleast one thread no exclusive but further shared access possible)<\/td>\n<\/table>\n<hr>\n<p><strong>Locks<\/strong> (Selection)<br \/>\n&#8230; operates on a mutex for automatic locking<\/p>\n<table>\n<col width=\"25%\">\n<col width=\"75%\">\n<tr>\n<td>std::lock_guard<\/td>\n<td>Pulls the mutex from its construction til its destruction<\/td>\n<tr>\n<td>std::unique_lock<\/td>\n<td>Pulls the mutex, but its interface allows to un\/lock it manually as well (necessary for the usage of wait on conditional variables)<\/td>\n<tr>\n<td>std::shared_lock<\/td>\n<td>Pulls a shared mutex for multiple access as long as no exclusive lock has been given by that mutex<\/td>\n<\/table>\n<hr>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;string&gt;\r\n#include &lt;thread&gt;\r\n#include &lt;shared_mutex&gt;\r\n\r\nclass FileIO\r\n{\r\npublic:\r\n  FileIO(std::string file) : m_file(file) {}\r\n  void print() {\r\n    std::shared_lock&lt;std::shared_mutex&gt; lock(m_smtx); \/\/ shared lock\r\n    \/\/ print file\r\n  }\r\n  void write() {\r\n    std::lock_guard&lt;std::shared_mutex&gt; lock(m_smtx); \/\/ exclusive lock\r\n    \/\/ write file\r\n  }\r\nprivate:\r\n  std::string m_file;\r\n  std::shared_mutex m_smtx;\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Mutexes (Selection) &#8230; synchronizes the access on critical sections std::mutex Provides exclusive access (can only be pulled by one thread<\/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\/11978"}],"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=11978"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11978\/revisions"}],"predecessor-version":[{"id":16739,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11978\/revisions\/16739"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11978"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}