내가 할려던게 무한루프돌리는 스레드 만드는거고, 아래 코드 비슷함
리서치코드임 각종 예외상황 배제함 (아까 내코드에 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() ;
}
http://stackoverflow.com/questions/34898201/should-a-boolean-flag-always-be-atomic
레퍼런스는 항상 atomic을 써야 하는거구나... 이건 몰랐네
http://stackoverflow.com/questions/16111663/do-i-have-to-use-atomicbool-for-exit-bool-variable
심지어 컴파일러는 atomic 사용 여부를 최적화 기준으로 삼기도 한다네.. 충격이다
끼에에에에엑!!
플래그가 레지스터에 올라가는거땜에 그렇다네;
일방적으로 읽고 한객체만 쓰는상황이니까 굳이 아토믹은 상관없을텐데 그렇다네요
꿀링크감사 - dc App