1

I use Qt 4.8.6, MS Visual Studio 2008, Windows 7. I've created a GUI program. It contains main GUI thread and worker thread (I have not made QThread subclass, by the way), which makes synchronous calls to 3rd party DLL functions. These functions are rather slow. QTcpServer instance is also under worker thread. My worker class contains QTcpServer and DLL wrapper methods.

I know that quit() is preferred over terminate(), but I don't wanna wait for a minute (because of slow DLL functions) during program shutdown. When I try to terminate() worker thread, I notice warnings about stopping QTcpServer from another thread. What is a correct way of process shutdown?

ilya
  • 1,103
  • 14
  • 36
  • Is there an overriding reason to not call ExitProcess(0); ? – Martin James Apr 01 '15 at 07:31
  • @MartinJames: Only every single reason ever? `ExitProcess()` is a Windows-specific syscall, for one. The two methods [ilya](https://stackoverflow.com/users/831104/ilya) referenced (i.e., `quit` and `terminate`) are Qt-specific public methods of the `QThread` class. The question pertains to high-level platform-portable Qt worker thread shutdown, whereas your comment and [corresponding "answer"](https://stackoverflow.com/a/29389748/2809027) pertain only to low-level non-portable C and C++ process closure. The intersection of these two topics is the empty set. *You completely missed the point.* – Cecil Curry Aug 08 '18 at 01:23

3 Answers3

2

QThread::quit tells the thread's event loop to exit. After calling it the thread will get finished as soon as the control returns to the event loop of the thread

You may also force a thread to terminate right now via QThread::terminate(), but this is a very bad practice, because it may terminate the thread at an undefined position in its code, which means you may end up with resources never getting freed up and other nasty stuff. So use this only if you really can't get around it.

So i think the right approach is to first tell the thread to quit normally and if something goes wrong and takes much time and you have no way to wait for it, then terminate it:

QThread * th = myWorkerObject->thread();
th->quit();
th->wait(5000); // Wait for some seconds to quit

if(th->isRunning()) // Something took time more than usual, I have to terminate it
    th->terminate();
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • OP is asking about process termination. Unless there is a positive and overriding reason to do so, user code should not attempt to stop process threads. It's not very good at it, and the OS is very good. – Martin James Apr 01 '15 at 11:03
  • 1
    @MartinJames I can read that the title says: "Terminating Qt worker thread during program shutdown". I think he is asking about thread termination as he is talking about `quit`and `terminate`. – Nejat Apr 01 '15 at 11:13
  • **This is the only meaningful response.** Why every *other* response completely ignored the repeated references to **Qt** in the original question and instead pontificated on their own pet admonishments (e.g., ["Userland Thread Control Considered Harmful!"](https://stackoverflow.com/a/29389748/2809027)), we may truly never know. Let this be an age-old lesson to us all: think before typing, please. – Cecil Curry Aug 08 '18 at 01:45
0

You should always try to avoid killing threads from the outside by force and instead ask them nicely to finish what they're doing. This usually means that the thread checks regularly if it should terminate itself and the outside world tells it to terminate when needed (by setting a flag, signaling an event or whatever is appropriate for the situation at hand).

When a thread is asked to terminate itself, it finishes up what it's doing and exists cleanly. The application waits for the thread to terminate and then exits.

You say that in your case the thread takes a long time to finish. You can take this into consideration and still terminate the thread "the nice way" (for example you can hide the application window and give the impression that the app has exited, even if the process takes a little more time until it finally terminates; or you can show some form of progress indication to the user telling him that the application is shutting down).

Ionut
  • 6,436
  • 1
  • 17
  • 17
  • Not really, no, not at process-termination time, unless there is an overriding reason for attempting it. – Martin James Apr 01 '15 at 11:01
  • While slightly more useful than [Martin James'](https://stackoverflow.com/users/758133/martin-james) [diabolical screed against userland thread termination](https://stackoverflow.com/a/29389748/2809027), this answer still fails to supply a valid solution. The two methods [ilya](https://stackoverflow.com/users/831104/ilya) referenced (i.e., `quit` and `terminate`) are Qt-specific public methods of the `QThread` class. This answer fails to even reference Qt, thus constituting yet another non-answer. `` – Cecil Curry Aug 08 '18 at 01:40
-1

Unless there is an overriding reason to do so, you should not attempt to terminate threads with user code at process-termination.

If there is no such reason, just call your OS process termination syscall, eg. ExitProcess(0). The OS can, and will will stop all process threads in any state before releasing all process resources. User code cannot do that, and should not try to terminate threads, or signal them to self-terminate, unless absolutely necessary.

Attempting to 'clean up' with user code sounds 'nice', (aparrently), but is an expensive luxury that you will pay for with extra code, extra testing and extra maintenance.

That is, if your customers don't stop buying your app because they get pissed off with it taking so long to shut down.

The OS is very good at stopping threads and cleaning up. It's had endless thousands of hours of testing during development and decades of life in the wild where problems with process termination would have become aparrent and got fixed. You will not even get close to that with your flags, events etc. as you struggle to stop threads running on another core without the benefit of an interprocessor driver.

There are surely times when you will have to resort to user code to stop threads. If you need to stop them before process termination, or you need to close some DB connection, flush some file at shutdown, deal with interprocess comms or the like issues, then you will have to resort to some of the approaches already suggested in other answers.

If not, don't try to duplicate OS functionality in the name of 'niceness'. Just ask it to terminate your process. You can get your warm, fuzzy feeling when your app shuts down immedately while other developers are still struggling to implement 'Shutdown' progress bars or trying to explain to customers why they have 15 zombie apps still running.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • Every StackOverflow post has its "What You're Doing Is Considered Harmful" bait-and-switch answer that fails to actually answer anything. This is that answer. `ExitProcess` is a Windows-specific syscall. The two methods [ilya](https://stackoverflow.com/users/831104/ilya) referenced (i.e., `quit`, `terminate`) are Qt-specific public methods of the `QThread` class. The question pertains to high-level platform-portable Qt worker thread shutdown, whereas your non sequitur response pertains only to low-level non-portable C and C++ process termination. The two have nothing to do with one another. – Cecil Curry Aug 08 '18 at 01:28