並è¡å¦çãå®ç¾ããæ©è½ã¨ãã¦æåã«è§¦ããã®ãOSã®ã¹ã¬ããï¼ãã¨ã¡ã¢ãªä¿è·ã®ç¡ãç°å¢ã®ãªã¢ã«ã¿ã¤ã OSã®ã¿ã¹ã¯ï¼ã§ããããã®ããããã¹ã¬ããã®çæã»çµäºã®ã³ã¹ããæ°ã«ãªã£ã¦ãã¾ããã¯ã³ã·ã§ããã®å¦çãã¹ã¬ããã§è¡ãããï¼âé »ç¹ã«ã¹ã¬ãããçæãã¦çµäºãããï¼ãã¨ã«ã¤ãã¦ãæªã ã«æµæãããã
ï¼è»½éã¹ã¬ããã®é¡ã使ããç°å¢ã«ãªããã¨ãããã¨ããããâ¦â¦ï¼
ãããªè¨³ã§ãä¸åº¦çæããã¹ã¬ããã使ãåãã³ã¼ããæ¸ããã¨ãå¤ããã¹ã¬ããã®ä¸ã§ã«ã¼ãããã¦ããã¦ãå¤é¨ãããå®è¡ããå¦çããä¾é ¼ããæ§é ã ã
ã¯ã©ã¹ã®ä¸ã«éãè¾¼ããã®ãªãã°ãã³ã³ã¹ãã©ã¯ã¿ã§ã¹ã¬ãããçæãã¦ããã¹ãã©ã¯ã¿ã§çµäºããããã¨ãã£ãæãã«ãªãã
ããã§ããã¹ãã©ã¯ã¿ã«ããã¦ãã©ããã£ã¦ã¹ã¬ãããçµäºããããï¼ãã¨ããåé¡ãçãããé »ç¹ã«ã¹ã¬ããå¤ããã¹ã¬ããå ã«å¦çãä¾é ¼ããã±ã¼ã¹ã§ã¯ãé«ç¢ºçã§ã¹ã¬ããééä¿¡ã®ä»çµã¿ãç¨æãããã¨ã«ãªããããããã«ä¹ã£ãã£ã¦ãã¹ã¬ãããçµäºãããã¨ããã¡ãã»ã¼ã¸ãéãã°ãããã§ãããã®ãããªä»çµã¿ãç¨æããã¦ããªãã®ãªããã©ãã ãããï¼
ä¼çµ±çãªã¹ã¬ããã»ããã°ã©ãã³ã°ã«ä¸æ £ããªéçºè ãæ¸ãã¦ãã¾ããã¡ãªã³ã¼ãã¯ããããªæãã ãããï¼
#include <chrono> #include <iostream> #include <thread> using namespace std::literals::chrono_literals; using std::this_thread::sleep_for; class Foo final { private: bool m_stop { false }; std::thread m_thread; public: Foo() { using std::cout, std::endl; m_thread = std::thread([&]{ cout << "start thread" << endl; while (!m_stop) { cout << "loop" << endl; sleep_for(1ms); } cout << "stop thread" << endl; }); } ~Foo() { m_stop = true; m_thread.join(); } };
ã¹ã¬ããã®çµäºãéç¥ããããã«bool
åã®å¤æ°m_stopãç¨ãã¦ãããã¹ã¬ããå
é¨ã§ãå¨æçã«ãã®å¤æ°ã®å¤åãç£è¦ãã訳ã ã
ãã®ã³ã¼ãã«ã¯åé¡ããããC++ã®ä»æ§ã«å°ãã°ãã詳ãããªãã°ãããã¡ã¢ãªä¸ã®ãªãã¸ã§ã¯ãã«ãããã¦æä»å¶å¾¡ç¡ãã§ãå¤ãåç §ããã¹ã¬ãããã¨ãå¤ãå¤æ´ããã¹ã¬ããããåå¨ããã³ã¼ãã¯æªå®ç¾©ã®åä½ï¼ãã¼ã¿ç«¶åï¼ãå¼ãèµ·ãããã¨ãããã¨ã«æ°ã¥ãã ããã
å®ç´ã«ç´ããªããä¾ãã°å¤æ°m_stopãbool
åã§ã¯ãªãstd::atomic_bool
åã«ããã°ããã ããã
#include <atomic> #include <chrono> #include <iostream> #include <thread> using namespace std::literals::chrono_literals; using std::this_thread::sleep_for; class Foo final { private: std::atomic_bool m_stop { false }; std::thread m_thread; public: Foo() { using std::cout, std::endl; m_thread = std::thread([&]{ cout << "start thread" << endl; while (!m_stop.load()) { cout << "loop" << endl; sleep_for(1ms); } cout << "stop thread" << endl; }); } ~Foo() { m_stop.store(true); m_thread.join(); } };
ãã1ã¤æ°ã«ãªããã¨ããããã¹ã¬ããã®ä¸ã§while (!m_stop)
ã¨ãã£ãæãã§m_stopãç£è¦ãã¦ãã訳ã ããã¹ã¬ããã®ä¸ã«ããã¹ã¬ãããçæãã¦ããã³ã³ã¹ãã©ã¯ã¿ã®ä¸ã«ããå¤æ°m_stopãæ¸ãæããã³ã¼ãã1ã¤ãåå¨ããªãã
ãã®ãããããããããã³ã³ãã¤ã«æã«æ¬¡ã®ãããªã³ã¼ãã«æé©åããã¦ãã¾ããããããªããã¨ããç念ãçããã
cout << "start thread" << endl; if (m_stop) { cout << "stop thread" << endl; return; } while (1) { cout << "loop" << endl; sleep_for(1ms); }
ãã®ãããªæé©åãéªéããã«ã¯volatile
ã使ãã°ããã ããã
#include <chrono> #include <iostream> #include <thread> using namespace std::literals::chrono_literals; using std::this_thread::sleep_for; class Foo final { private: volatile bool m_stop { false }; std::thread m_thread; public: Foo() { using std::cout, std::endl; m_thread = std::thread([&]{ cout << "start thread" << endl; while (!m_stop) { cout << "loop" << endl; sleep_for(1ms); } cout << "stop thread" << endl; }); } ~Foo() { m_stop = true; m_thread.join(); } };
std::atomic
ã¨volatile
ã¯å¥ã®æ©è½ã§ããã¤ä½µç¨ãããã¨ãå¯è½ã ãä»åã®ã±ã¼ã¹ã§ã¯å¤æ°m_stopãvolatile std::atomic_bool
åã«ããã°ããããã ã
#include <atomic> #include <chrono> #include <iostream> #include <thread> using namespace std::literals::chrono_literals; using std::this_thread::sleep_for; class Foo final { private: volatile std::atomic_bool m_stop { false }; std::thread m_thread; public: Foo() { using std::cout, std::endl; m_thread = std::thread([&]{ cout << "start thread" << endl; while (!m_stop.load()) { cout << "loop" << endl; sleep_for(1ms); } cout << "stop thread" << endl; }); } ~Foo() { m_stop.store(true); m_thread.join(); } };
ããã§çµããã ãããï¼ãåé¡ãªãåä½ããããã«è¦ããã³ã¼ãã ããåæã«ãã¡ãã£ã¨ã°ããæ³¥èãã³ã¼ãã«ãè¦ãããããå°ãã¹ãã¼ããªæ¹æ³ã¯ãªãã ãããï¼
æè¿æ°ã¥ããã®ã¯ã1åéãã®ã¹ã¬ããééä¿¡ã«ã¯std::future
ã¨std::promise
ã使ãã°ãããã¨ãããã¨ã ã
#include <chrono> #include <future> #include <iostream> #include <thread> using namespace std::literals::chrono_literals; using std::this_thread::sleep_for; class Foo final { private: std::promise<void> m_pr_stop; std::thread m_thread; public: Foo() { using std::cout, std::endl; m_thread = std::thread([&, fu_stop = std::move(m_pr_stop.get_future())]{ cout << "start thread" << endl; while (fu_stop.wait_for(0ms) == std::future_status::timeout) { cout << "loop" << endl; sleep_for(1ms); } cout << "stop thread" << endl; }); } ~Foo() { m_pr_stop.set_value(); m_thread.join(); } };
å
ã®ã³ã¼ãã®æ§é ãä¿æããããã«ãwhileæã®å¤å®å¼ã§ã¯wait_for(0ms)
ã§ã¹ãªã¼ãããããwhileæã®æå¾ã§sleep_for(1ms)
ãã¦ãããååã®å¦çã®ã¿ã¤ãã³ã°ãå¤å°é
ãã¦ãæ§ããªãã®ãªãã°ãwait_for(1ms)
ã«ãã¦æ«å°¾ã®sleep_for(1ms)
ãåãé¤ãã¦ãã¾ãã°ããã ããã