c++ - When a process ends, what happens to a thread that is in the middle of Sleep()? -


I am working with a class that starts and ends a thread. Thread is created in the manufacturer Thread function contains a loop that continues until a flag is TRUE. The flag is a stable member of the class. Disinvestor set the flag false. In this way, each instance of the class has an associated thread that runs for example life.

I'm trying to wrap my head when the deconstructor moves, and if it's okay to finish the thread I do not have much experience with multithreading.

Here I think that this happens, inside the deconstructor, the flag will be set to false. Let's sleep (sleep) (running for sleep). The object is destroyed but the flag is still present in memory because it is stable but the whole process is finished, we say, so at some point the static flag will disappear. Will the flag disappear before the thread? If the thread is forced to return to the end of the process, does the thread take care of any more flag? I do not know what happens at this point.

I am using Visual C ++ in Visual Studio 2010.

Note that the runaway destroyer, while some other thread uses that object (not static stag) It will generate undefined results.

Think about what will happen when the thread is in the middle of processing, instead of looking at the flag, processing is a very thin chance.

It is better to type a stop (bull waiting) function, so if the destructor is a call and it is necessary to clean the auto, you can stop and press "Stop" until the other flags Block until you set up, or just join pthread_join in that thread (not recommended, see below).

Also, when you block for a beautiful finish, you set one timeout, and if something goes wrong, then end the thread (and generate an alarm for debug).


Comments