I wanted to have some kind of delegation class. An abridged version of my approach is below, and the main function is to start a new thread that performs some actions (in this example, it prints text every second):
void Flusher::start(){ m_continue.store(true); m_thread = std::thread([](std::atomic<bool>& shouldContinue){ while(shouldContinue.load()){ std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "sec passed" << std::endl; }}, std::ref<std::atomic<bool>>(m_continue) ); }
My concern is that the std :: thread constructor has the following signature:
template< class Function, class... Args > explicit thread( Function&& f, Args&&... args );
Thus, the rvalue reference is required as the first and second arguments. If so, then I should not use shouldContinue after passing it to the std::thread constructor since it was moved .
Of course, I want to have control over this function, and therefore I want to use shouldContinue in the caller's thread to stop the called function. For obvious reasons, I do not want this variable to be global.
I think std::ref does some magic there, but I'm still not sure how it works (I saw std::ref in some examples when creating a new thread).
I tried not to worry about the fact, this is an rvalue reference, and I later used shouldContinue , and nothing crashed, but I am afraid that this is just undefined behavior. Can anyone tell if the code above is correct, and if not, how to do it right?