{"id":5685,"date":"2026-05-11T18:40:36","date_gmt":"2026-05-11T09:40:36","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5685"},"modified":"2026-05-11T18:55:11","modified_gmt":"2026-05-11T09:55:11","slug":"basicthread2-detach-join","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/11\/basicthread2-detach-join\/","title":{"rendered":"[Basic]Thread2 &#8211; detach, join"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \uc2a4\ub808\ub4dc\uc5d0\uc11c detach\uc640 join \uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.<br>Below is an explanation of detach and join in threads.<\/p>\n\n\n\n<p>\u2714\ufe0f detach<\/p>\n\n\n\n<p>&#8212; detach\ubc29\uc2dd\uc740 \uc2a4\ub808\ub4dc \ub3d9\uc791\uc744 \ubc31\uadf8\ub77c\uc6b4\ub4dc\uc5d0\uc11c \uc644\uc804\ud788 \ubd84\ub9ac\uc2dc\ud0b5\ub2c8\ub2e4.<br>The detach method completely separates thread operations from the background.<\/p>\n\n\n\n<p>&#8212; \uc644\uc804\ud788 \ubd84\ub9ac\ub418\uc5b4 \ub3c5\ub9bd\uc801\uc73c\ub85c \ub3d9\uc791\ud558\uae30 \ub54c\ubb38\uc5d0 \uc885\ub8cc\uc2dc\uc810\uc744 \uc54c\uae30 \uc5b4\ub835\uace0 \uc81c\uc5b4\uac00 \uc5b4\ub835\uc2b5\ub2c8\ub2e4.<br>Because they are completely separated and operate independently, it is difficult to know the termination point and difficult to control.<\/p>\n\n\n\n<p>&#8212; \ube44\uad50\uc801 \uac04\ub2e8\ud55c \uc791\uc5c5\uc5d0 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>It is used for relatively simple tasks.<\/p>\n\n\n\n<p>\u2714\ufe0f join<\/p>\n\n\n\n<p>&#8212; join()\uc740 joinable()\uc774 true\uc774\uba74 \uc2a4\ub808\ub4dc\ub97c \ub300\uae30\uc0c1\ud0dc\ub85c \ub9cc\ub4dc\ub294 \ud568\uc218\uc785\ub2c8\ub2e4.<br>join() is a function that puts a thread into a waiting state if joinable() is true.<\/p>\n\n\n\n<p><br>&#8212; join\uc744 \uc0ac\uc6a9\ud558\ub294 \ubc29\uc2dd\uc740 \uc2a4\ub808\ub4dc\uc758 \uc885\ub8cc\uc9c0\uc810\uc744 \uad00\ub9ac \uac00\ub2a5\ud558\uae30\ub54c\ubb38\uc5d0 \ubcf5\uc7a1\ud55c \uc11c\ubc84\ub098 \ub300\uaddc\ubaa8 \ud504\ub85c\uadf8\ub7a8\uc5d0\uc11c \uc0ac\uc6a9\ub429\ub2c8\ub2e4.<br>The join method is used in complex servers or large-scale programs because it allows for the management of thread termination points.<\/p>\n\n\n\n<p>\u2714\ufe0f \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>&#8212; detach.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;thread&gt;\n\nvoid worker(int id) {\n    std::cout &lt;&lt; \"Worker \" &lt;&lt; id &lt;&lt; \" started.\\n\";\n    std::this_thread::sleep_for(std::chrono::seconds(1));\n    std::cout &lt;&lt; \"Worker \" &lt;&lt; id &lt;&lt; \" finished.\\n\";\n}\n\nint main() {\n    for(int i = 0; i &lt; 3; ++i) {\n        std::thread t(worker, i);\n\n        \/\/ \uc2a4\ub808\ub4dc\ub97c \ubd84\ub9ac\ud558\uc5ec \ub3c5\ub9bd \uc2e4\ud589\n        \/\/ Separate threads for independent execution\n        t.detach();  \n    }\n    \n    std::cout &lt;&lt; \"Main thread finished without waiting.\\n\";\n    \/\/ \uba54\uc778 \uc2a4\ub808\ub4dc\uac00 \uc5ec\uae30\uc11c \uc885\ub8cc\ub418\uc5b4\ub3c4 worker \uc2a4\ub808\ub4dc\ub294 \ubc31\uadf8\ub77c\uc6b4\ub4dc\uc5d0\uc11c \uacc4\uc18d \ub3d9\uc791\ud560 \uc218 \uc788\uc74c\n    \/\/ Even if the main thread terminates here, the worker thread can continue to run in the background\n\n    \/\/ \uc2a4\ub808\ub4dc\uac00 \uc791\uc5c5 \ud560 \uc2dc\uac04 \ud655\ubcf4\uc6a9(\ub514\ubc84\uae45 \ubaa9\uc801)\n    \/\/ To secure time for the thread to work (for debugging purposes)\n    std::this_thread::sleep_for(std::chrono::seconds(2)); \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>&#8212; vt.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;thread&gt;\n#include &lt;vector&gt;\n\nvoid worker(int id) {\n    std::cout &lt;&lt; \"Worker \" &lt;&lt; id &lt;&lt; \" started.\\n\";\n    std::this_thread::sleep_for(std::chrono::seconds(1));\n    std::cout &lt;&lt; \"Worker \" &lt;&lt; id &lt;&lt; \" finished.\\n\";\n}\n\nint main() {\n    std::vector&lt;std::thread&gt; threads;\n\n    for(int i = 0; i &lt; 3; ++i) {\n        std::thread t(worker, i);\n\n        \/\/ \uc2a4\ub808\ub4dc \uac1d\uccb4\ub97c \ubca1\ud130\uc5d0 \uc800\uc7a5\n        \/\/ Store thread objects in a vector\n        threads.push_back(std::move(t));  \n    }\n    \n    \/\/ \ubaa8\ub4e0 \uc2a4\ub808\ub4dc\uac00 \uc885\ub8cc\ub420 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9bc\n    \/\/ Wait until all threads terminate\n    for(auto &amp;t : threads) {\n        if(t.joinable()) {\n            t.join();\n        }\n    }\n\n    \/\/ \ubca1\ud130\uc5d0\uc11c \uc2a4\ub808\ub4dc \uac1d\uccb4 \uc81c\uac70\n    \/\/ Remove thread objects from vector\n    threads.clear(); \n    std::cout &lt;&lt; \"All workers completed.\\n\";\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ucef4\ud30c\uc77c \/ Compiling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>g++ vt.cpp -o vt -std=c++17 -pthread\ng++ detach.cpp -o detach -std=c++17 -pthread<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f\uc2e4\ud589 \/ Run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/vt\n.\/detach<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \uc2a4\ub808\ub4dc\uc5d0\uc11c detach\uc640 join \uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.Below is an explanation of detach and join in threads. \u2714\ufe0f detach &#8212; detach\ubc29\uc2dd\uc740 \uc2a4\ub808\ub4dc \ub3d9\uc791\uc744 \ubc31\uadf8\ub77c\uc6b4\ub4dc\uc5d0\uc11c \uc644\uc804\ud788 \ubd84\ub9ac\uc2dc\ud0b5\ub2c8\ub2e4.The detach method completely separates thread operations from the background. &#8212; \uc644\uc804\ud788 \ubd84\ub9ac\ub418\uc5b4 \ub3c5\ub9bd\uc801\uc73c\ub85c \ub3d9\uc791\ud558\uae30 \ub54c\ubb38\uc5d0 \uc885\ub8cc\uc2dc\uc810\uc744 \uc54c\uae30 \uc5b4\ub835\uace0 \uc81c\uc5b4\uac00 \uc5b4\ub835\uc2b5\ub2c8\ub2e4.Because they are completely separated and operate independently, it is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5685","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5685"}],"version-history":[{"count":1,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5685\/revisions"}],"predecessor-version":[{"id":5686,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5685\/revisions\/5686"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}