{"id":5440,"date":"2026-04-24T14:00:00","date_gmt":"2026-04-24T05:00:00","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5440"},"modified":"2026-04-24T12:59:06","modified_gmt":"2026-04-24T03:59:06","slug":"basic-thread-reference","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/04\/24\/basic-thread-reference\/","title":{"rendered":"[Basic]\uc2a4\ub808\ub4dc,\ucc38\uc870\/Thread,Reference"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\ub808\ub4dc\ub294 \ud558\ub098\uc758 \ud504\ub85c\uadf8\ub7a8 \ub0b4\uc5d0\uc11c \uc5ec\ub7ec \uc791\uc5c5\uc744 \ub3d9\uc2dc\uc5d0 \ucc98\ub9ac\ud558\ub294 \uac83\ucc98\ub7fc \ubcf4\uc774\uac8c \ud558\ub294 \uae30\ubc95\uc785\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \ud504\ub85c\uadf8\ub7a8\uc774 \ud55c \uc791\uc5c5\uc774 \ub05d\ub0a0 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9ac\ub294 \ub3d9\uc548 \ub2e4\ub978 \uc791\uc5c5\uc744 \ucc98\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. <br>Threads are a technique that makes it appear as though multiple tasks are being processed simultaneously within a single program. This allows the program to handle other tasks while waiting for one task to finish.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb thread.cpp\uc5d0\uc11c \u2b50\ufe0f \ubd80\ubd84\uc740  \ubcc0\uc218\ucc38\uc870(&amp;)\ub97c \uc0ac\uc6a9\ud558\uc9c0\ub9cc \ubb38\uc790\uc5f4\uc744 \ub300\uc785\ud558\uace0 \uc788\uc73c\ub098 \uc624\ub958\uac00 \ubc1c\uc0dd\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br>In thread.cpp, the \u2b50\ufe0f part uses a variable reference (&amp;) but assigns a string, yet no error occurs.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb reference.cpp\ucf54\ub4dc\ub97c \ud1b5\ud574\uc11c  \uc774\uc720\ub97c \ud655\uc778 \ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>You can check the reason through the reference.cpp code.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucc38\uc870\ub97c (&amp;)\uc0ac\uc6a9\ud558\uc9c0\ub9cc const\ub97c \uc0ac\uc6a9\ud574\uc11c \uc77d\uae30\ub9cc \ud558\uace0 \ub0b4\uc6a9 \uc218\uc815\uc744 \ud558\uc9c0 \uc54a\ub294\ub2e4\uace0 \ubcf4\uc7a5\ud568\uc73c\ub85c\uc368 \uba54\ubaa8\ub9ac\uc5d0\uc11c \ubb38\uc790\uc5f4\uc774 \uc0ac\ub77c\uc9c4 \ud6c4 \ucc38\uc870\ub420 \uc704\ud5d8\uc774 \uc5c6\uc5b4\uc9d1\ub2c8\ub2e4.<br>By using references (&amp;) but using const to guarantee that the content is read-only and not modified, the risk of the string being referenced after it has disappeared from memory is eliminated.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void worker_fun2(const std::string&amp; msg){\n    std::cout &lt;&lt; \"Worker 1: \" &lt;&lt; msg &lt;&lt; \"\\n\"&lt;&lt; std::endl;\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>\u2714\ufe0fthread.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \ucf58\uc194\uc5d0 \uba54\uc2dc\uc9c0\ub97c \ucd9c\ub825\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n\/\/ You can print messages to the console.\n#include &lt;iostream&gt; \n\n\/\/ \uc4f0\ub808\ub4dc\ub97c \uc0dd\uc131\ud558\uace0 \uad00\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.\n\/\/ You can create and manage threads.\n#include &lt;thread&gt; \n\n\/\/ \uc2dc\uac04 \uc9c0\uc5f0\uc744 \uc704\ud574 \ud544\uc694\ud569\ub2c8\ub2e4, \uc2dc\uac04 \uad00\ub828 \uae30\ub2a5\uc744 \uc81c\uacf5\ud569\ub2c8\ub2e4.\n\/\/ It is needed for time delay, and provides time-related functions.\n#include &lt;chrono&gt; \n\n\/\/ 1. \uc4f0\ub808\ub4dc\uc5d0\uc11c \uc2e4\ud589\ub420 \ud568\uc218 \uc815\uc758\n\/\/ Define a function that will be executed in the thread\nvoid worker_function(int id, const std::string&amp; message, int delay_seconds) {\n\n    std::cout &lt;&lt; \"Worker &#91;\" &lt;&lt; id &lt;&lt; \"] \uc774 \uc791\uc5c5\uc744 \uc2dc\uc791\ud569\ub2c8\ub2e4. \uba54\uc2dc\uc9c0: \" &lt;&lt; message &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"Worker &#91;\" &lt;&lt; id &lt;&lt; \"] Starting this job. Message: \" &lt;&lt; message &lt;&lt; \"\\n\" &lt;&lt; std::endl;\n    \n    \/\/ \uc9c0\uc815\ub41c \uc2dc\uac04 \ub3d9\uc548 \ub300\uae30 \n    \/\/ Wait for a specified amount of time \n    std::this_thread::sleep_for(std::chrono::seconds(delay_seconds));\n    \n    std::cout &lt;&lt; \"Worker &#91;\" &lt;&lt; id &lt;&lt; \"] \uc791\uc5c5 \uc644\ub8cc. \uc2dc\uac04 \uacbd\uacfc: \" &lt;&lt; delay_seconds &lt;&lt; \"\ucd08.\\n\";\n    std::cout &lt;&lt; \"Worker &#91;\" &lt;&lt; id &lt;&lt; \"] Task completed. Time elapsed: \" &lt;&lt; delay_seconds &lt;&lt; \" seconds.\\n\";\n}\n\nint main() {\n\n    std::cout &lt;&lt; \"--- Thread Start---\\n\";\n\n    \/\/ 2. std::thread \uac1d\uccb4 \uc0dd\uc131 \ubc0f \ud568\uc218 \uc2e4\ud589\n    \/\/ Create std::thread objects and execute the function\n\n    \/\/ std::thread(\ud568\uc218 \ud3ec\uc778\ud130, \uc778\uc7901, \uc778\uc7902, ...) \ud615\ud0dc\ub85c \uc0ac\uc6a9\ud569\ub2c8\ub2e4.\n    \/\/ You can use std::thread in the form of std::thread(function pointer, arg1, arg2, ...)\n\n\n    \/\/ Worker 1 \uc2a4\ub808\ub4dc \uc0dd\uc131 \n    \/\/ Create Worker 1 thread \u2b50\ufe0f\n    std::thread t1(worker_function, 1, \"frist background work\", 3);\n    \n    \/\/ Worker 2 \uc2a4\ub808\ub4dc \uc0dd\uc131 (t1\uacfc \ub3d9\uc2dc\uc5d0 \uc2e4\ud589\ub428)\n    \/\/ Create Worker 2 thread (runs concurrently with t1) \u2b50\ufe0f\n    std::thread t2(worker_function, 2, \"second background work\", 1);\n\n    \/\/ 3. \uc4f0\ub808\ub4dc \uc2dc\uc791 \ud6c4 \uba54\uc778 \uc2a4\ub808\ub4dc\uac00 \uc989\uc2dc \ub2e4\uc74c \ucf54\ub4dc\ub97c \uc2e4\ud589\ud568\uc744 \ud655\uc778\n    std::cout &lt;&lt; \"\\n&#91;Main Thread] \ub450 \uac1c\uc758 \uc4f0\ub808\ub4dc\ub97c \uc131\uacf5\uc801\uc73c\ub85c \uc2dc\uc791\ud588\uc2b5\ub2c8\ub2e4.\\n\";\n    std::cout &lt;&lt; \"&#91;Main Thread] \uc4f0\ub808\ub4dc\ub4e4\uc774 \ub3cc\uc544\uac00\ub3c4\ub85d \uae30\ub2e4\ub9ac\ub294 \ub3d9\uc548, \uba54\uc778 \uc2a4\ub808\ub4dc\ub294 \ub2e4\ub978 \uc791\uc5c5\uc744 \uc218\ud589\ud569\ub2c8\ub2e4.\\n\";\n    std::cout &lt;&lt; \"\\n&#91;Main Thread] Two threads started successfully.\\n\";\n    std::cout &lt;&lt; \"&#91;Main Thread] While waiting for the threads to return, the main thread performs other tasks.\\n\";\n    std::cout &lt;&lt; \"\\n\";\n    \n    \/\/ 4. join()\uc744 \uc0ac\uc6a9\ud558\uc5ec \uc4f0\ub808\ub4dc\uac00 \ub05d\ub0a0 \ub54c\uae4c\uc9c0 \uba54\uc778 \uc2a4\ub808\ub4dc\ub97c \ub300\uae30\uc2dc\ud0b5\ub2c8\ub2e4.\n    \/\/ Use join() to make the main thread wait until the threads finish.\n\n    \/\/ \ub9cc\uc57d join()\uc744 \uc0ac\uc6a9\ud558\uc9c0 \uc54a\uace0 \ud504\ub85c\uadf8\ub7a8\uc774 \uc885\ub8cc\ub418\uba74, \uc4f0\ub808\ub4dc\ub294 \uac15\uc81c \uc885\ub8cc\ub420 \uc704\ud5d8\uc774 \uc788\uc2b5\ub2c8\ub2e4.\n    \/\/ If you do not use join() and the program ends, there is a risk that the threads will be forcibly terminated.\n    \n    \/\/ t1\uc774 \ub05d\ub0a0 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9bc\n    \/\/ Wait for t1 to finish\n    t1.join(); \n    \n    \/\/ t2\uac00 \ub05d\ub0a0 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9bc\n    \/\/ Wait for t2 to finish\n    t2.join();\n\n    \/\/ \ubaa8\ub4e0 \uc4f0\ub808\ub4dc\uac00 \ub05d\ub09c \ud6c4\uc5d0 \uc2e4\ud589\ub418\ub294 \ucf54\ub4dc\n    \/\/ Code that runs after all threads have completed\n    std::cout &lt;&lt; \"--- Thread End ---\\n\";\n\n    return 0;\n}\n\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++ thread.cpp -o thread -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>.\/thread<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uacb0\uacfc \/ Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thread % .\/thread\n--- Thread Start---\nWorker &#91;1] \uc774 \uc791\uc5c5\uc744 \uc2dc\uc791\ud569\ub2c8\ub2e4. \uba54\uc2dc\uc9c0: frist background work\nWorker &#91;1] Starting this job. Message: frist background work\n\n\n&#91;Main Thread] \ub450 \uac1c\uc758 \uc4f0\ub808\ub4dc\ub97c \uc131\uacf5\uc801\uc73c\ub85c \uc2dc\uc791\ud588\uc2b5\ub2c8\ub2e4.\n&#91;Main Thread] \uc4f0\ub808\ub4dc\ub4e4\uc774 \ub3cc\uc544\uac00\ub3c4\ub85d \uae30\ub2e4\ub9ac\ub294 \ub3d9\uc548, \uba54\uc778 \uc2a4\ub808\ub4dc\ub294 \ub2e4\ub978 \uc791\uc5c5\uc744 \uc218\ud589\ud569\ub2c8\ub2e4.\n\n&#91;Main Thread] Two threads started successfully.\n&#91;Main Thread] While waiting for the threads to return, the main thread performs other tasks.\n\nWorker &#91;2] \uc774 \uc791\uc5c5\uc744 \uc2dc\uc791\ud569\ub2c8\ub2e4. \uba54\uc2dc\uc9c0: second background work\nWorker &#91;2] Starting this job. Message: second background work\n\nWorker &#91;2] \uc791\uc5c5 \uc644\ub8cc. \uc2dc\uac04 \uacbd\uacfc: 1\ucd08.\nWorker &#91;2] Task completed. Time elapsed: 1 seconds.\nWorker &#91;1] \uc791\uc5c5 \uc644\ub8cc. \uc2dc\uac04 \uacbd\uacfc: 3\ucd08.\nWorker &#91;1] Task completed. Time elapsed: 3 seconds.\n--- Thread End ---\n thread % <\/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++ reference.cpp -o ref -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>.\/ref<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uacb0\uacfc \/ Result<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thread % .\/ref\nWorker 1: hi\n\nWorker 1: Hello, Silverhand~~ !\n\nWorker 1: aloy~~\ud83d\udc4b\n\nthread % <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc2a4\ub808\ub4dc\ub294 \ud558\ub098\uc758 \ud504\ub85c\uadf8\ub7a8 \ub0b4\uc5d0\uc11c \uc5ec\ub7ec \uc791\uc5c5\uc744 \ub3d9\uc2dc\uc5d0 \ucc98\ub9ac\ud558\ub294 \uac83\ucc98\ub7fc \ubcf4\uc774\uac8c \ud558\ub294 \uae30\ubc95\uc785\ub2c8\ub2e4. \uc774\ub97c \ud1b5\ud574 \ud504\ub85c\uadf8\ub7a8\uc774 \ud55c \uc791\uc5c5\uc774 \ub05d\ub0a0 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9ac\ub294 \ub3d9\uc548 \ub2e4\ub978 \uc791\uc5c5\uc744 \ucc98\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. Threads are a technique that makes it appear as though multiple tasks are being processed simultaneously within a single program. This allows the program to handle other tasks [&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],"tags":[],"class_list":["post-5440","post","type-post","status-publish","format-standard","hentry","category-cpp","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5440","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=5440"}],"version-history":[{"count":3,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5440\/revisions"}],"predecessor-version":[{"id":5442,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5440\/revisions\/5442"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}