I use std::thread in my C++ code to constantly poll some data and add it to the buffer. I use C++ lambda to start the stream as follows:
StartMyThread() { thread_running = true; the_thread = std::thread { [this] { while(thread_running) { GetData(); } }}; }
thread_running is an atomic<bool> declared in the class header. Here is my GetData function:
GetData() { //Some heavy logic which needs to be executed in a worker thread }
Further, I also have a StopMyThread function, where I set thread_running to false so that it thread_running while loop in the lambda block .
StopMyThread() { thread_running = false; the_thread.join(); }
It works well. The thread starts and stops without fail.
This C ++ code is used on iOS, Android, OS X, and Windows. There is a button in my user interface that requires me to start and stop the flow with the click of a button; This button can often be used in some cases. I see a delay per second in the user interface when a thread stops or starts.
My question is: In C ++, is this the correct way to start / stop a thread often? I think that with this logic, I create a new thread every time. And, as I understand it, creating a new thread forces the OS to allocate a lot of new resources, which may be temporary. And I think this is the mistake I am making. How can i avoid this?
How to use the same stream without highlighting it once throughout the entire life cycle of the application, and just play / pause it if necessary?
source share