I got an interesting question a while ago: should an application terminate (anonymous) threads or not?
- [WayBack] delphi – How do I reliably wait on a thread that has just been created? – Stack Overflow: you cannot as Execute might not be called, and there is a race condition on
Started
. - [WayBack] Debugging Multithreaded Applications with Delphi:
WaitFor
might not return (which you cannot get around this because all overloads of theCheckThreadError
are non-virtual) - [WayBack] delphi – terminate all the threads (TThread) on closing application – Stack Overflow
- [WayBack] multithreading – How to terminate anonymous threads in Delphi on application close? – Stack Overflow: you cannot because referring to any of those threads is a race condition.
A problem is that a thread might not execute unless you call WaitFor
before Terminate
is called. The reason is that the internal function ThreadProc
does not start Execute
if the thread is already terminated.
The
ThreadProc
in theSystem.Classes
unit is an ideal place to set breakpoints in order to see which threads might start.Other useful places to set breakpoints:
TAnonymousThread.Execute
TExternalThread.Execute
Execute
not being called by ThreadProc
is a bug, but it is not documented because QC is gone (taking the below entry with it), it is not in QP and the docwiki never got updated.
Given QC has so much information, I am still baffled that Embarcadero took it down.
Sergey Kasandrov (a.k.a. serg or sergworks) wrote in [WayBack] Sleep sort and TThread corner case | The Programming Works about this bug and refers to WayBack: QualityCentral 35451 – TThread implementation doesn’t guarantee that thread’s Execute method will be called at all .
The really bad thing are the WayBack: QualityCentral Resolution Entries for Report #35451 Resolution “As Designed” implying the design is wrong.
In his post, sergworks implemented the Sleep sorting in Delphi. Related:
- WayBack: Genius sorting algorithm: Sleep sort
- [WayBack] Reddit – 4chan: Sleep sort : programming
- [WayBack] 4chan: Sleep sort : programming
- [WayBack] Sorting algorithms/Sleep sort – Rosetta Code
- [WayBack] What is the time complexity of the sleep sort? – Stack Overflow
Note that application shutdown is a much debated topic. Best is to do as little cleanup as possible: your process is going to terminate soon anyway. No need to close handles or free memory: Windows will do that for you anyway. See for instance:
- [WayBack] The old-fashioned theory on how processes exit – The Old New Thing
- [WayBack] Quick overview of how processes exit on Windows XP – The Old New Thing
- [WayBack] Changes to power management in Windows Vista – The Old New Thing
- [WayBack] Now that Windows makes it harder for your program to block shutdown, how do you block shutdown? – The Old New Thing
- [WayBack] A process shutdown puzzle – The Old New Thing
- [WayBack] A process shutdown puzzle: Answers – The Old New Thing
- [WayBack] A process shutdown puzzle, Episode 2 – The Old New Thing
- [WayBack] Why does Internet Explorer not call DLL_PROCESS_DETACH on my DLL when I call ExitProcess? – The Old New Thing
- [WayBack] When DLL_PROCESS_DETACH tells you that the process is exiting, your best bet is just to return without doing anything – The Old New Thing
- [WayBack] Why can’t I delete a file immediately after terminating the process that has the file open? – The Old New Thing
- [WayBack] During process termination, the gates are now electrified – The Old New Thing
- [WayBack] How my lack of understanding of how processes exit on Windows XP forced a security patch to be recalled – The Old New Thing
- [WayBack] The old-fashioned theory on how processes exit – The Old New Thing
- [WayBack] Some reasons not to do anything scary in your DllMain – The Old New Thing
Related to waiting:
Related to executing:
- [WayBack] TThread.Create Constructor
- [WayBack] CheckThreadError Method
- [WayBack] TThread.Destroy Destructor
- [WayBack] TThread.Execute Method
- [WayBack] TThread.OnTerminate Event
- [WayBack] TThread.Resume Method
- [WayBack] TThread.Suspend Method
- [WayBack] TThread.Terminate Method
- [WayBack] TThread.Terminated Property
- [WayBack] TThread Members
- [WayBack] System.Classes.TThread.Started – RAD Studio API Documentation
–jeroen