multithreading - C++ How to correctly use threads with a while loop -
I'm new to thread, but I've been reading this for the past few days and now I'm trying to make it one To implement in the actual example.
I have a GUI class that should start a thread on the click of a button. My implementation is following:
zero interface :: on_startbutton_clicked () {theDetector.start (); } Zero Interface: on_stopButton_clicked () {// Not sure how to stop the thread}
The detector class has the following code:
zero Detector :: Start () {thread T1 (and detector :: detection, thread, this); T1.detach (); } Zero Detector :: Detect Thread () {isActive = true;
I think this is not correct The way to do this is, if I separate the thread, then I can not stop it through boolean and if I am involved in it immediately after being suspected by the GUI, what would be the right way to do this example?
should be a designer
a std :: unique_ptr & lt; Std :: thread & gt; PThread;
and a std :: atomic & lt; Bool & gt; Halt;
.
Start
if there is a pThread
then nothing should be done. If not, then halt = false; PThread.reset (New Study :: Thread (and Detector :: Task, it));
Do not separate - which is rarely a good idea.
In the Prevent
, set halt = true;
then if (pThread) {pThread-> Include (); PThread.reset (); }
In the detector :: task
, loop while (! Halt)
.
If code is more complex in code, and a single loop can be too long to wait for the UI release, you can join
to a different method > Would like to postpone.
You would also like to add the detector :: ~ detector ()
to stop / add / reset the task.
Comments
Post a Comment