Make the thread a member of the class, and instead of detaching it in the constructor, join it in the destructor. To stop the thread from looping, you can have a boolean inside the class that signals whether the thread should continue running or not (std::atomic<bool> update
).
The thread could be executing this: [this] { while (update) threadUpdate(); }
.
In the destructor of your class, do update = false
, and call thread.join()