简体   繁体   English

当进程结束时,Sleep()中间的线程会发生什么?

[英]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. 我正在使用一个开始和结束线程的类。 The thread is created in the constructor. 线程在构造函数中创建。 The thread function has a loop that continues as long as a flag is TRUE. 线程函数有一个循环,只要标志为TRUE就会继续。 The flag is a static member of the class. 该标志是该类的静态成员。 The deconstructor sets the flag to FALSE. 解构函数将标志设置为FALSE。 This way, each instance of the class has an associated thread that runs for the lifetime of the instance. 这样,类的每个实例都有一个关联的线程,该线程在实例的生命周期内运行。

I am trying to wrap my head around what happens when the deconstructor runs, and if this is an okay way to end the thread. 我试图绕过解构器运行时发生的事情,如果这是一个结束线程的好方法。 I do not have much experience with multithreading. 我对多线程没有多少经验。

Here's what I think happens. 这就是我认为发生的事情。 Inside the deconstructor, the flag will be set to FALSE. 在解构器内部,标志将设置为FALSE。 Let's assume Sleep() is running for infinity. 我们假设Sleep()正在为无穷大运行。 The object is destroyed, but the flag still exists in memory because it is static. 该对象被销毁,但该标志仍然存在于内存中,因为它是静态的。 But the entire process is ending, let's say, so at some point the static flag will disappear. 但是,整个过程即将结束,所以在某些时候,静态标志将会消失。 Will the flag disappear before the thread? 国旗会在线程之前消失吗? If the thread is forced to return by the process ending, does the thread even care about the flag any more? 如果线程被进程结束强制返回,那么线程是否会再关心该标志? I don't know what takes place at this point. 我不知道此时发生了什么。

I am using Visual C++ in Visual Studio 2010. 我在Visual Studio 2010中使用Visual C ++。

On Windows a thread has a lifetime which is less than or equal to the process in which it is cerated. 在Windows上,线程的生命周期小于或等于其被调用的过程。 Hence once the process ends so does the thread. 因此,一旦过程结束,线程也就这样结束。

A normal process shutdown won't complete until all of the threads in that process terminate. 在该进程中的所有线程终止之前,将无法完成正常的进程关闭。 Hence in this case the flag will be set, the main thread may terminate but the created background threads will continue running. 因此,在这种情况下将设置标志,主线程可以终止,但创建的后台线程将继续运行。 Eventually they will see the FALSE value of the flag, exit their loop, complete and the process shutdown will be complete. 最终,他们将看到标志的FALSE值,退出循环,完成并且过程关闭将完成。

Note that running destructor while some other thread is still in use of that object (not the static flag) will produce undefined result. 请注意,当一些其他线程仍在使用该对象(而不是静态标志)时运行析构函数将产生未定义的结果。

Think what happen when the thread is in middle of processing instead of checking the alive flag, pretty slim chance huh. 想一想当线程处于处理中间而不是检查活动标志时会发生什么,相当渺茫的机会吧。

It is better to write a stop(bool wait) function, so if destructor is call and required to auto clean up, you set the flag to stop, and block until the thread to set another flag to "stopped", or just join that thread with pthread_join (not recommended, see below). 最好写一个stop(bool wait)函数,所以如果析构函数被调用并需要自动清理,你可以设置标志为stop,并阻塞直到线程将另一个标志设置为“stopped”,或者只是加入使用pthread_join的线程(不推荐,见下文)。

Furthermore, when you block for graceful termination you may also set a timeout, and force terminate the thread if something goes wrong (and generate alarm for debug). 此外,当您阻止正常终止时,您也可以设置超时,并在出现错误时强制终止线程(并为调试生成警报)。

It depends on whether the 'deconstructor' actually runs at all. 这取决于'解构者'是否实际上运行。

If you attempt to 'deconstruct' the objects you describe in some 'OnClose' event-handler, setting the static flag will instruct the threads to terminate if they get around to checking it. 如果你试图“解构”你在某个'OnClose'事件处理程序中描述的对象,设置静态标志将指示线程在它们检查它时终止。 If they do not, the threads will continue to sleep, (or remain stuck on whatever call is blocking, or continue to run code). 如果他们不这样做,线程将继续休眠,(或者仍然停留在阻塞的任何呼叫上,或者继续运行代码)。

If you take no further action to wait for object-thread termination, the main GUI thread will run on, destroy all its GUI objects etc. and and call ExitProcess(). 如果您不采取进一步操作等待对象线程终止,主GUI线程将运行,销毁其所有GUI对象等,并调用ExitProcess()。

Once ExitProcess() is called, the OS will stop ALL threads of the process, no matter what state they are in, before deallocating any memory, (like the memory containing your flag). 一旦调用了ExitProcess(),操作系统将在释放任何内存之前停止进程的所有线程,无论它们处于什么状态,(如包含您的标志的内存)。

The thread that calls ExitProcess() never has control returned to it. 调用ExitProcess()的线程永远不会有控制权返回给它。 Other threads that belong to the same process, and are not running on another core, have their state set so that they are never run again. 属于同一进程但未在另一个核心上运行的其他线程的状态设置为永不再运行。 Other threads that belong to the same process, and are running on another core, have the core that they are running on hardware-interrupted to stop the threads. 属于同一进程并在另一个核心上运行的其他线程具有在硬件上运行的核心,以阻止线程。

Will the flag disappear before the thread? 国旗会在线程之前消失吗?

No. The thread/s will be stopped before the memory hosting the flag is deallocated. 不会。在释放托管标志的内存之前,线程将被停止。

If you do not wish this forced-termination to occur, you must take action to await the actual termination of the object-threads. 如果您不希望发生强制终止,则必须采取措施等待对象线程的实际终止。 You have to delay the main GUI-thread from calling ExitProcess by setting an appropriate CloseAction in the OnClose handler. 您必须通过在OnClose处理程序中设置适当的CloseAction来延迟主GUI线程调用ExitProcess。 The GUI thread should only close/release itself when all the object-threads have terminated and the objects' destructors have completed. 当所有对象线程都已终止且对象的析构函数已完成时,GUI线程应仅关闭/释放自身。

If I really, really, really have to do this, prefer to do it by PostMessaging a 'WM_THREADGONE' Windows message to the main GUI thread, (as the last action of the object-threads before actually terminating themselves), and counting down a 'threadCount' towards zero in the message-handler, so keeping the GUI thread available to handle messages until all object-threads have terminated themselves. 如果我真的,真的,真的必须这样做,宁愿通过PostMessaging一个'WM_THREADGONE'Windows消息到主GUI线程,(作为实际终止自己之前的对象线程的最后一个动作),并倒计时消息处理程序中的'threadCount'为零,因此保持GUI线程可用于处理消息,直到所有对象线程都自行终止。 Hard-Wait mechanisms, like Join(), are just deadlock-generators that have a huge capacity for shutdown problems and should not be used. Hard-Wait机制(如Join())只是死锁生成器,具有很大的关闭问题容量,不应该使用。

I usually try to work around all thread termination issues by designing my apps so that sudden, involuntary thread termination is an acceptable action then and let Exitprocess() blow everything away. 我通常尝试通过设计我的应用来解决所有线程终止问题,以便突然的,不自觉的线程终止是一个可接受的操作,然后让Exitprocess()将所有内容都吹走。 This is not always possible, but it's a pile safer if you can get away with it. 这并不总是可行的,但是如果你可以侥幸逃脱它,它就更安全了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当函数执行结束时,向量中的线程会发生什么? - What happens to a thread in a vector when function execution ends? 当分叉的进程死亡时,该进程中的分离线程会如何处理? - What happens to a detached thread inside a forked process when the process dies? 当我使用boost :: this_thread :: sleep时,/ proc / {pid}中其pid目录如何处理? - when I use boost::this_thread::sleep, what happens to its pid directory in /proc/{pid}? 当返回对象的函数在没有return语句的情况下结束时会发生什么 - What happens when a function that returns an object ends without a return statement 当 main() 退出时,分离的线程会发生什么? - What happens to a detached thread when main() exits? 在 Qt 中,当线程完成时对象会发生什么? - In Qt, what happens to an object when a thread finishes? 构造线程时会发生什么,以及线程是如何执行的 - What happens when a thread is constructed, and how is the thread executed WinEventHook:安装事件挂钩的线程结束时会发生什么? - WinEventHook: what happen when the thread that installed the event hook ends? 线程中sleep()函数的作用是什么? - What is the purpose of the sleep() function in a Thread? 当一个线程 forks() 时,其他线程会发生什么? - What happens to other threads when one thread forks()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM