{"id":11544,"date":"2026-04-02T13:31:39","date_gmt":"2026-04-02T04:31:39","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=11544"},"modified":"2026-04-02T13:31:39","modified_gmt":"2026-04-02T04:31:39","slug":"c-%eb%a9%80%ed%8b%b0%ec%8a%a4%eb%a0%88%eb%93%9c%ec%9d%98-%ea%b8%b0%ec%b4%88-mutex%eb%ae%a4%ed%85%8d%ec%8a%a4-%ea%b0%80%ec%9d%b4%eb%93%9c","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=11544","title":{"rendered":"C++ &#8211; \uba40\ud2f0\uc2a4\ub808\ub4dc\uc758 \uae30\ucd08: Mutex(\ubba4\ud14d\uc2a4) \uac00\uc774\ub4dc"},"content":{"rendered":"<h1>C++ &#8211; \uba40\ud2f0\uc2a4\ub808\ub4dc\uc758 \uae30\ucd08: Mutex(\ubba4\ud14d\uc2a4) \uac00\uc774\ub4dc<\/h1>\n<p>\uba40\ud2f0\uc2a4\ub808\ub4dc \ud504\ub85c\uadf8\ub798\ubc0d\uc744 \ud558\ub2e4 \ubcf4\uba74 \uc5ec\ub7ec \uc2a4\ub808\ub4dc\uac00 \ud558\ub098\uc758 \ubcc0\uc218\ub098 \uac1d\uccb4\uc5d0 \ub3d9\uc2dc\uc5d0 \uc811\uadfc\ud558\ub294 \uc0c1\ud669\uc774 \ubc1c\uc0dd\ud569\ub2c8\ub2e4. \uc774\ub54c \ub370\uc774\ud130\uac00 \uae68\uc9c0\ub294 \uac83\uc744 \ubc29\uc9c0\ud558\uae30 \uc704\ud574 \uc0ac\uc6a9\ud558\ub294 \uac00\uc7a5 \uae30\ucd08\uc801\uc778 \ub3c4\uad6c\uac00 \ubc14\ub85c <strong>Mutex(\ubba4\ud14d\uc2a4)<\/strong>\uc785\ub2c8\ub2e4.<\/p>\n<h2>1. Mutex\ub780 \ubb34\uc5c7\uc778\uac00?<\/h2>\n<p><strong>Mutex<\/strong>\ub294 <strong>Mut<\/strong>ual <strong>Ex<\/strong>clusion(\uc0c1\ud638 \ubc30\uc81c)\uc758 \uc57d\uc790\uc785\ub2c8\ub2e4.<\/p>\n<p>\uc27d\uac8c \ube44\uc720\ud558\uc790\uba74, \uc5ec\ub7ec \uc0ac\ub78c\uc774 \uc774\uc6a9\ud558\ub294 <strong>\ud654\uc7a5\uc2e4\uc758 \ub2e8 \ud558\ub098\ubfd0\uc778 \uc5f4\uc1e0<\/strong>\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<ul>\n<li>\n<p>\uc5f4\uc1e0\uac00 \uc788\ub294 \uc0ac\ub78c\ub9cc \ud654\uc7a5\uc2e4(\uacf5\uc720 \uc790\uc6d0)\uc5d0 \ub4e4\uc5b4\uac08 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<\/li>\n<li>\n<p>\uc548\uc5d0 \uc0ac\ub78c\uc774 \uc788\ub2e4\uba74, \ubc16\uc758 \uc0ac\ub78c\ub4e4\uc740 \uc5f4\uc1e0\uac00 \ubc18\ub0a9\ub420 \ub54c\uae4c\uc9c0 \uc904\uc744 \uc11c\uc11c \uae30\ub2e4\ub824\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<\/li>\n<\/ul>\n<h2>2. \ud575\uc2ec \ud568\uc218: lock()\uacfc unlock()<\/h2>\n<p>C++\uc5d0\uc11c <code>std::mutex<\/code>\ub97c \uc0ac\uc6a9\ud560 \ub54c \uac00\uc7a5 \ud575\uc2ec\uc774 \ub418\ub294 \ub450 \uac00\uc9c0 \ud568\uc218\uc785\ub2c8\ub2e4.<\/p>\n<h3>lock() : \uc5f4\uc1e0 \ud68d\ub4dd (\uc7a0\uadf8\uae30)<\/h3>\n<ul>\n<li>\n<p>\uc774 \ud568\uc218\ub97c \ud638\ucd9c\ud55c \uc2a4\ub808\ub4dc\ub294 \ubba4\ud14d\uc2a4\uc758 \uc18c\uc720\uad8c\uc744 \uac00\uc838\uc624\ub824\uace0 \uc2dc\ub3c4\ud569\ub2c8\ub2e4.<\/p>\n<\/li>\n<li>\n<p>\ub9cc\uc57d \ub2e4\ub978 \uc2a4\ub808\ub4dc\uac00 \uc774\ubbf8 <code>lock()<\/code>\uc744 \ud574\uc11c \uc0ac\uc6a9 \uc911\uc774\ub77c\uba74, \uadf8 \uc2a4\ub808\ub4dc\uac00 <code>unlock()<\/code>\uc744 \ud560 \ub54c\uae4c\uc9c0 <strong>\ud604\uc7ac \uc2a4\ub808\ub4dc\ub294 \uc774 \ub77c\uc778\uc5d0\uc11c \uba48\ucdb0(Blocking)<\/strong> \uae30\ub2e4\ub9bd\ub2c8\ub2e4.<\/p>\n<\/li>\n<\/ul>\n<h3>unlock() : \uc5f4\uc1e0 \ubc18\ub0a9 (\ud480\uae30)<\/h3>\n<ul>\n<li>\n<p>\uacf5\uc720 \uc790\uc6d0 \uc0ac\uc6a9\uc774 \ub05d\ub0ac\uc74c\uc744 \uc54c\ub9ac\uace0 \uc5f4\uc1e0\ub97c \uc81c\uc790\ub9ac\uc5d0 \ub461\ub2c8\ub2e4.<\/p>\n<\/li>\n<li>\n<p>\uc774\uc81c \uc904 \uc11c\uc11c \uae30\ub2e4\ub9ac\ub358 \ub2e4\ub978 \uc2a4\ub808\ub4dc \uc911 \ud558\ub098\uac00 \uc5f4\uc1e0\ub97c \uc9d1\uc5b4 \ub4e4\uace0 <code>lock()<\/code>\uc744 \ud1b5\uacfc\ud560 \uc218 \uc788\uac8c \ub429\ub2c8\ub2e4.<\/p>\n<\/li>\n<\/ul>\n<h2>3. \ucf54\ub4dc \uc608\uc2dc: std::deque \ubcf4\ud638\ud558\uae30<\/h2>\n<p>commandQueue &#8211; commandQueueMutex \ub294 \uc2e4\uc9c8\uc801\uc73c\ub85c \uc5f0\uad00\uad00\uacc4\ub294 \uc874\uc7ac\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>commandQueueMutex.lock();<\/code> \ubd80\ubd84\uc744 \ub77d\uc744 \uac78\uc5b4\uc11c \ub2e4\ub978 \uc2a4\ub798\ub4dc\uc5d0\uc11c <code>commandQueueMutex.lock();<\/code> \ub97c \uc2e4\ud589\ud558\ub824 \ud560 \ub54c \uba48\ucd94\uac8c \ud569\ub2c8\ub2e4.<\/p>\n<p>\ucd5c\ub300\ud55c \ube60\ub974\uac8c <code>unlock()<\/code> \uc744 \ud558\uc9c0 \uc54a\uc73c\uba74, \ub370\ub4dc\ub77d\uc774 \ub429\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;deque&gt;\n#include &lt;mutex&gt;\n#include &lt;thread&gt;\n\n\/\/ \uacf5\uc720 \uc790\uc6d0\uacfc \uc774\ub97c \ubcf4\ud638\ud560 \ubba4\ud14d\uc2a4\nstd::deque&lt;int&gt; commandQueue;\nstd::mutex commandQueueMutex;\n\nvoid producer() {\n    for (int i = 0; i &lt; 5; ++i) {\n        \/\/ 1. \uc811\uadfc \uc804 \uc7a0\uae08\n        commandQueueMutex.lock();\n\n        std::cout &lt;&lt; &quot;Pushing: &quot; &lt;&lt; i &lt;&lt; std::endl;\n        commandQueue.push_back(i);\n\n        \/\/ 2. \uc791\uc5c5 \ud6c4 \ubc18\ub4dc\uc2dc \uc7a0\uae08 \ud574\uc81c\n        commandQueueMutex.unlock();\n\n        std::this_thread::sleep_for(std::chrono::milliseconds(100));\n    }\n}\n\nvoid consumer() {\n    while (true) {\n        commandQueueMutex.lock(); \/\/ \uc811\uadfc \uc804 \uc7a0\uae08\n\n        if (!commandQueue.empty()) {\n            int data = commandQueue.front();\n            commandQueue.pop_front();\n            std::cout &lt;&lt; &quot;Popping: &quot; &lt;&lt; data &lt;&lt; std::endl;\n        }\n\n        commandQueueMutex.unlock(); \/\/ \uc791\uc5c5 \ud6c4 \ud574\uc81c\n\n        \/\/ \ub370\uc774\ud130\uac00 \uc5c6\uc73c\uba74 \uc7a0\uc2dc \ub300\uae30 (CPU \uc810\uc720\uc728 \ubc29\uc9c0)\n        std::this_thread::sleep_for(std::chrono::milliseconds(150));\n    }\n}<\/code><\/pre>\n<h2>4. \uc8fc\uc758\uc0ac\ud56d: \ubba4\ud14d\uc2a4 \uc0ac\uc6a9 \uc2dc \uc9c0\ucf1c\uc57c \ud560 \uaddc\uce59<\/h2>\n<p>\ubba4\ud14d\uc2a4\ub294 \ubb3c\ub9ac\uc801\uc73c\ub85c \ub370\uc774\ud130\ub97c \uc7a0\uadf8\ub294 \uac83\uc774 \uc544\ub2c8\ub77c, <strong>\ud504\ub85c\uadf8\ub798\uba38\ub4e4 \uc0ac\uc774\uc758 \uc57d\uc18d<\/strong>\uc785\ub2c8\ub2e4. \ub530\ub77c\uc11c \ub2e4\uc74c \uaddc\uce59\uc744 \ubc18\ub4dc\uc2dc \uc9c0\ucf1c\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<ol>\n<li>\n<p><strong>\ub3d9\uc77c\ud55c \uc5f4\uc1e0 \uc0ac\uc6a9:<\/strong> <code>commandQueue<\/code>\ub97c \ubcf4\ud638\ud558\uae30\ub85c \ud588\ub2e4\uba74, \uc774 \ud050\uc5d0 \uc811\uadfc\ud558\ub294 \ubaa8\ub4e0 \uacf3\uc5d0\uc11c <strong>\ubc18\ub4dc\uc2dc \ub3d9\uc77c\ud55c<\/strong> <code>commandQueueMutex<\/code> \uac1d\uccb4\ub97c \uc0ac\uc6a9\ud574\uc57c \ud569\ub2c8\ub2e4.<\/p>\n<\/li>\n<li>\n<p><strong>\ubc18\ub4dc\uc2dc unlock() \ud638\ucd9c:<\/strong> <code>lock()<\/code>\ub9cc \ud558\uace0 <code>unlock()<\/code>\uc744 \ud558\uc9c0 \uc54a\uc73c\uba74, \ud504\ub85c\uadf8\ub7a8\uc740 \uc601\uc6d0\ud788 \uba48\ucdb0\ubc84\ub9ac\ub294 <strong>\ub370\ub4dc\ub77d(Deadlock)<\/strong> \uc0c1\ud0dc\uc5d0 \ube60\uc9d1\ub2c8\ub2e4.<\/p>\n<\/li>\n<li>\n<p><strong>\ucd5c\uc18c\ud55c\uc758 \uc601\uc5ed\ub9cc \uc7a0\uadf8\uae30:<\/strong> <code>lock()<\/code>\uacfc <code>unlock()<\/code> \uc0ac\uc774\uc758 \ucf54\ub4dc\uac00 \ub108\ubb34 \uae38\uc5b4\uc9c0\uba74 \ub2e4\ub978 \uc2a4\ub808\ub4dc\ub4e4\uc774 \ub108\ubb34 \uc624\ub798 \uae30\ub2e4\ub824\uc57c \ud558\ubbc0\ub85c, \uaf2d \ud544\uc694\ud55c \uacf5\uc720 \uc790\uc6d0 \uc811\uadfc \ubd80\ubd84\ub9cc \uac10\uc2f8\ub294 \uac83\uc774 \uc88b\uc2b5\ub2c8\ub2e4.<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>C++ &#8211; \uba40\ud2f0\uc2a4\ub808\ub4dc\uc758 \uae30\ucd08: Mutex(\ubba4\ud14d\uc2a4) \uac00\uc774\ub4dc \uba40\ud2f0\uc2a4\ub808\ub4dc \ud504\ub85c\uadf8\ub798\ubc0d\uc744 \ud558\ub2e4 \ubcf4\uba74 \uc5ec\ub7ec \uc2a4\ub808\ub4dc\uac00 \ud558\ub098\uc758 \ubcc0\uc218\ub098 \uac1d\uccb4\uc5d0 \ub3d9\uc2dc\uc5d0 \uc811\uadfc\ud558\ub294 \uc0c1\ud669\uc774 \ubc1c\uc0dd\ud569\ub2c8\ub2e4. \uc774\ub54c \ub370\uc774\ud130\uac00 \uae68\uc9c0\ub294 \uac83\uc744 \ubc29\uc9c0\ud558\uae30 \uc704\ud574 \uc0ac\uc6a9\ud558\ub294 \uac00\uc7a5 \uae30\ucd08\uc801\uc778 \ub3c4\uad6c\uac00 \ubc14\ub85c Mutex(\ubba4\ud14d\uc2a4)\uc785\ub2c8\ub2e4. 1. Mutex\ub780 \ubb34\uc5c7\uc778\uac00? Mutex\ub294 Mutual Exclusion(\uc0c1\ud638 \ubc30\uc81c)\uc758 \uc57d\uc790\uc785\ub2c8\ub2e4. \uc27d\uac8c \ube44\uc720\ud558\uc790\uba74, \uc5ec\ub7ec \uc0ac\ub78c\uc774 \uc774\uc6a9\ud558\ub294 \ud654\uc7a5\uc2e4\uc758 \ub2e8 \ud558\ub098\ubfd0\uc778 \uc5f4\uc1e0\uc640 \uac19\uc2b5\ub2c8\ub2e4. \uc5f4\uc1e0\uac00 \uc788\ub294 \uc0ac\ub78c\ub9cc \ud654\uc7a5\uc2e4(\uacf5\uc720 \uc790\uc6d0)\uc5d0 \ub4e4\uc5b4\uac08\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=11544\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-11544","post","type-post","status-publish","format-standard","hentry","category-c-c"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11544"}],"version-history":[{"count":1,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11544\/revisions"}],"predecessor-version":[{"id":11545,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/11544\/revisions\/11545"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}