{"id":5558,"date":"2026-05-01T11:00:00","date_gmt":"2026-05-01T02:00:00","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5558"},"modified":"2026-05-01T11:12:06","modified_gmt":"2026-05-01T02:12:06","slug":"basic-escape-loop-using-commands","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/01\/basic-escape-loop-using-commands\/","title":{"rendered":"[Basic]\uba85\ub839\uc5b4\ub85c \ub8e8\ud504\ud0c8\ucd9c\/Escape loop using commands"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \ud130\ubbf8\ub110\uc5d0\uc11c \uba87\uac00\uc9c0 \uba85\ub839\uc5b4\ub85c while\ub8e8\ud504\ub97c \ud0c8\ucd9c\ud558\ub294 \ub85c\uc9c1\uc785\ub2c8\ub2e4.<br>Below is the logic to exit a while loop using a few commands in the terminal.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb run_cmd() \ud568\uc218 \ub0b4\uc5d0\uc11c \/exit \uba85\ub839\uc5b4\uac00 \ub4e4\uc5b4\uc624\uba74 running\ud50c\ub798\uadf8\ub97c false\ub85c \ubc14\uafd4\uc11c \ub8e8\ud504\ub97c \ud0c8\ucd9c\ud569\ub2c8\ub2e4.<br>If the \/exit command is encountered inside the run_cmd() function, the running flag is changed to false to exit the loop.<\/p>\n\n\n\n<p id=\"tw-target-text\">\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>\u2714\ufe0f Server.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;string>\n#include &lt;functional>\n#include &lt;vector>\n#include &lt;thread>\n#include &lt;atomic> \/\/ \u2757\ufe0f\n\/\/ -- Exit Logic -- \/\/\nclass Server {\npublic:\n    std::atomic&lt;bool> running{false};\n    std::function&lt;std::string(const std::string&amp;)> logic_handler;\n\n    void set_handler(std::function&lt;std::string(const std::string&amp;)> handler) {\n        logic_handler = handler;\n    }\n\n    void receive_request(const std::string&amp; message) {\n        std::cout &lt;&lt; \"&#91;Server] \uc694\uccad \ubc1b\uc74c\/Receive Request: \" &lt;&lt; message &lt;&lt; std::endl;\n        if (logic_handler) {\n            std::string response = logic_handler(message);\n            std::cout &lt;&lt; \"&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Send Response: \" &lt;&lt; response &lt;&lt; \"\\n\" &lt;&lt; std::endl;\n        }\n    }\n\n    \/\/ \uc11c\ubc84\uc758 \uba54\uc778 \ub85c\uc9c1\uc774 \ub3cc\uc544\uac00\ub294 \ub8e8\ud504\n    \/\/ Loop where the server's main logic runs\n    void run() {\n        std::cout &lt;&lt; \"&#91;INFO] \uba54\uc778 \ub8e8\ud504 \uc2dc\uc791...\" &lt;&lt; std::endl;\n        std::cout &lt;&lt; \"&#91;INFO] Main loop started...\" &lt;&lt; std::endl;\n        while (running.load()) { \/\/ \u2757\ufe0f\n            int count = 0;\n\n            \/\/ \uc2e4\uc81c \uc11c\ubc84\ub77c\uba74 \uc5ec\uae30\uc11c \ub124\ud2b8\uc6cc\ud06c \ud328\ud0b7\uc744 \uae30\ub2e4\ub9ac\uac70\ub098 \uc791\uc5c5\uc744 \ucc98\ub9ac\ud569\ub2c8\ub2e4.\n            \/\/ If it were a real server, it would wait for network packets or process tasks here.\n            std::this_thread::sleep_for(std::chrono::milliseconds(500));\n        }\n        std::cout &lt;&lt; \"&#91;INFO] \uba54\uc778 \ub8e8\ud504\uac00 \uc885\ub8cc\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\" &lt;&lt; std::endl;\n        std::cout &lt;&lt; \"&#91;INFO] The main loop has finished.\" &lt;&lt; std::endl;\n    }\n\n    void cmd_run() {\n        std::string command;\n        while (true) {\n            std::cout &lt;&lt; \"CMD> \";\n            if (!std::getline(std::cin, command)) break;\n\n            if (command.empty()) continue;\n\n            if (command == \"\/exit\" || command == \"exit\") {\n                std::cout &lt;&lt; \"&#91;INFO] \uc885\ub8cc \uba85\ub839 \uac10\uc9c0. \ud50c\ub798\uadf8\ub97c \ubcc0\uacbd\ud569\ub2c8\ub2e4.\\n\";\n                std::cout &lt;&lt; \"&#91;INFO] Exit command detected. Changing flags.\\n\";\n\n                \/\/ running \ud50c\ub798\uadf8\ub97c false\ub85c \ub9cc\ub4e4\uc5b4 run()\uc758 while\ubb38\uc744 \ud0c8\ucd9c\ud558\uac8c \ud568\n                \/\/ Set the running flag to false to exit the while loop in run()\n                running.store(false); \/\/ \u2757\ufe0f\n                break;\n            } else if (command == \"\/status\") {\n                std::cout &lt;&lt; \"&#91;STATUS] \uc2e4\ud589 \uc0c1\ud0dc\/running status: \" &lt;&lt; (running.load() ? \"Running\" : \"Stopped\") &lt;&lt; \"\\n\";\n            } else {\n\n                \/\/ \uc77c\ubc18 \uba85\ub839\uc5b4 \uc785\ub825 \uc2dc logic_handler \uc2e4\ud589 \uc608\uc2dc\n                \/\/ Example of executing logic_handler when a standard command is entered\n                receive_request(command);\n            }\n        }\n    }\n};\n\nstd::string router(const std::string&amp; msg) {\n    if (msg == \"aloy\") return \"horizon\";\n    if (msg == \"silverhand\") return \"cyberpunk\";\n    return \"unknown command\";\n}\n\nint main() {\n    Server my_sv;\n\n\/\/\n\/\/ \ub9ac\ud134(\ucd9c\ub825): \ub9e8 \uc55e\uc5d0 \uc788\ub294 std::string\n\/\/ \uc785\ub825(\uc778\uc790): \uad04\ud638 () \uc548\uc5d0 \uc788\ub294 const std::string&amp;\n\/\/ Return (Output): The std::string at the beginning\n\/\/ Input (Argument): The const std::string&amp; inside the parentheses ()\n\n\/\/  my_sv.set_handler(router);\n\n    my_sv.set_handler(&#91;&amp;](const std::string&amp; req) {\n        return router(req);\n    });\n\n    \/\/ \uc11c\ubc84\ub97c \uc2dc\uc791 \uc0c1\ud0dc\ub85c \ubcc0\uacbd \/ Set the server to running state\n    my_sv.running.store(true);\n\n    \/\/ run() \uba54\uc11c\ub4dc\ub97c \ubcc4\ub3c4\uc758 \uc4f0\ub808\ub4dc\uc5d0\uc11c \uc2e4\ud589 (\ube44\ub3d9\uae30) \/ Run the run() method in a separate thread (asynchronous)\n    std::thread worker(&amp;Server::run, &amp;my_sv);\n\n    \/\/ \uba54\uc778 \uc4f0\ub808\ub4dc\uc5d0\uc11c \uba85\ub839\uc5b4 \uc785\ub825 \ub300\uae30 \/ Wait for command input in the main thread\n    my_sv.cmd_run();\n\n    \/\/ cmd_run\uc774 \uc885\ub8cc\ub418\uba74 worker \uc4f0\ub808\ub4dc\uac00 \ub05d\ub0a0 \ub54c\uae4c\uc9c0 \uae30\ub2e4\ub9bc(join)\n    \/\/.Wait for the worker thread to finish when cmd_run exits\n    if (worker.joinable()) {\n        worker.join();\n    }\n\n    std::cout &lt;&lt; \"&#91;MAIN] \ud504\ub85c\uadf8\ub7a8\uc774 \uc644\uc804\ud788 \uc885\ub8cc\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\" &lt;&lt; std::endl;\n    std::cout &lt;&lt; \"&#91;MAIN] The program has completely terminated.\" &lt;&lt; std::endl;\n    return 0;\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++ server.cpp -o server -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>MacBookAir loop_exit_logic % .\/server\nCMD> &#91;INFO] \uba54\uc778 \ub8e8\ud504 \uc2dc\uc791...\n&#91;INFO] Main loop started...\n\nCMD> <strong>aloy<\/strong>\n&#91;Server] \uc694\uccad \ubc1b\uc74c\/Receive Request: aloy\n&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Send Response: horizon\n\nCMD><strong> test<\/strong>\n&#91;Server] \uc694\uccad \ubc1b\uc74c\/Receive Request: test\n&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Send Response: unknown command\n\nCMD> <strong>silverhand<\/strong>\n&#91;Server] \uc694\uccad \ubc1b\uc74c\/Receive Request: silverhand\n&#91;Server] \uc751\ub2f5 \ubcf4\ub0c4\/Send Response: cyberpunk\n\nCMD> <strong>\/exit<\/strong>\n&#91;INFO] \uc885\ub8cc \uba85\ub839 \uac10\uc9c0. \ud50c\ub798\uadf8\ub97c \ubcc0\uacbd\ud569\ub2c8\ub2e4.\n&#91;INFO] Exit command detected. Changing flags.\n&#91;INFO] \uba54\uc778 \ub8e8\ud504\uac00 \uc885\ub8cc\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\n&#91;INFO] The main loop has finished.\n&#91;MAIN] \ud504\ub85c\uadf8\ub7a8\uc774 \uc644\uc804\ud788 \uc885\ub8cc\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\n&#91;MAIN] The program has completely terminated.\nMacBookAir loop_exit_logic % <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc544\ub798\ub294 \ud130\ubbf8\ub110\uc5d0\uc11c \uba87\uac00\uc9c0 \uba85\ub839\uc5b4\ub85c while\ub8e8\ud504\ub97c \ud0c8\ucd9c\ud558\ub294 \ub85c\uc9c1\uc785\ub2c8\ub2e4.Below is the logic to exit a while loop using a few commands in the terminal. \ud83d\udc49\ud83c\udffb run_cmd() \ud568\uc218 \ub0b4\uc5d0\uc11c \/exit \uba85\ub839\uc5b4\uac00 \ub4e4\uc5b4\uc624\uba74 running\ud50c\ub798\uadf8\ub97c false\ub85c \ubc14\uafd4\uc11c \ub8e8\ud504\ub97c \ud0c8\ucd9c\ud569\ub2c8\ub2e4.If the \/exit command is encountered inside the run_cmd() function, the running flag is changed to false to exit the loop. [&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-5558","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\/5558","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=5558"}],"version-history":[{"count":10,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5558\/revisions"}],"predecessor-version":[{"id":5568,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5558\/revisions\/5568"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}