내가 할려던게 무한루프돌리는 스레드 만드는거고, 아래 코드 비슷함
리서치코드임 각종 예외상황 배제함 (아까 내코드에 startOptimizationThread() 한번만 호출하고 이후에 stopOptimizationThread() 호출함)

아래 예제코드있는데 그냥 bool이 아니고 std::atomic<bool> 씀
이거랑 비슷한게 그냥 bool 쓰면서 앞뒤로 mutex로 lock unlock 하는거 아님??

출처
http://www.cplusplus.com/forum/general/190480/

코드복붙
#include <iostream> #include <atomic> #include <thread> #include <chrono> void update( std::atomic<bool>& program_is_running, unsigned int update_interval_millisecs ) { const auto wait_duration = std::chrono::milliseconds(update_interval_millisecs) ; while( program_is_running ) { std::cout << "update stuff\n" << std::flush ; std::this_thread::sleep_for(wait_duration) ; } } int main() { std::cout << "*** press enter to exit the program gracefully\n\n" ; std::atomic<bool> running { true } ; const unsigned int update_interval = 50 ; // update after every 50 milliseconds std::thread update_thread( update, std::ref(running), update_interval ) ; // do other stuff in parallel: simulated below std::cin.get() ; // exit gracefully running = false ; update_thread.join() ; }