delphi - terminate all the threads (TThread) on closing application
Asked Answered
J

3

4

My application is a tcp/ip server, with main thread created only once & listening all the time. When new client connects, the main thread creates the new thread of TClientThread type. There is however no list of running Client threads, as that would make my app a bit complicated... is there any way to execute "terminate" method on all the threads, even if the thread is busy (in my case "busy" means it's waiting for the data, where the timeout set is about 30 sec ... so I have to kill it anyway, without waiting.)? The simple closing application seems not to run "terminate" method on the threads, which ends up with memory leaks reported by FastMM...

Juryman answered 29/7, 2009 at 21:32 Comment(0)
F
17

Memory leaks on shutdown are nothing to worry about - going to the trouble of freeing memory before returning control to the operating system is a waste of time and needlessly slows down application exit. All you really need to do is ensure that all data has been saved, and all interprocess handles (such as semaphores and mutexes) correctly released, and exit away.

For notifying clients, the best you can do would be a strategy somewhat like this:

  • Add all client-handling threads to some list somewhere (with suitable locking on creation, destruction and iteration)
  • Make client threads remove themselves from the list upon termination, and have the last item removed from the list set an event (manual reset event, e.g. TEvent in SyncObjs) if the server is shutting down
  • Introduce polling (e.g. select or equivalent with a timeout) or other kind of interruption (e.g. SO_RCVTIMEO / SO_SNDTIMEO) in what would otherwise be long-running blocking routines, monitoring the Terminated property
  • On shutdown, lock the list and iterate through it, calling Terminate, and then wait for the event to be signaled; of course, the listening socket which adds items to the list should be closed and known to be closed before iterating through the list
Foulmouthed answered 29/7, 2009 at 21:37 Comment(4)
well in fact the thread needs to be Terminated properly to let the app know to save it's client state as disconnected... so I need to know how to do that anyway!Juryman
Memory leaks on shutdown are nothing to worry about: maybe that's why I get that catastrophic failure almost every time I shut down the Delphi IDE.Solorio
Tihauan - on package unload is a different matter, for example, as packages might be repeatedly loaded and unloaded. As to why the IDE is crashing for you on shutdown, I can't speculate without more data.Foulmouthed
Memory leaks on shutdown ARE something to worry about. They are kind of signal, that you might have some problems in your code. You cannot ignore them BUT once you know why and when these leaks happen and that they are not dangerous you can accept them.Doge
A
2

Sounds like this article may help

What you'll see if you click that link:

Using Semaphores in Delphi, Part 2: The Connection Pool

By: Cary Jensen

Abstract: Semaphores are used to coordinate multiple threads and processes. That semaphores provide multiple threads with simultaneous access to a shared resource is highlighted by the TFixedConnectionPool class described in this article.

Arianearianie answered 29/7, 2009 at 21:46 Comment(1)
Semaphores are a synchronization construct and have little to do with the problem the OP has (terminating threads on shutdown)Hefter
S
2

I use a KillThreadList: TList global. I monitor it in my thread as:

while (Not Terminated) do
begin
  inc(Inker);
  if (WaitForSingleObject(FTick, finterval) = WAIT_TIMEOUT) then
  Begin
    if Inker >= 10 then
    Begin
      ProcessTables;
      Inker := 0;
      sleep(1000);
    End;
    if KillThreadList.Contains(ThreadID) = True then Terminate;
  End;
end;

I also test for the KillThreadList in my processes to let me opt out of them before completion, where safe to do so.

I pass the OnTerminate event out to the Main thread and remove the ThreadID from the KillList there. I use this model extensively and it has not failed me yet.

procedure TfrmProcessQualcommLocations.OnTerminateThread;
var
  ThreadID : Cardinal;
  i : integer;
  aStatusBar :TStatFrame;
begin
  ThreadID := (Sender as Tthread).ThreadID;
  for i := 0 to StatusBarList.Count -1  do
  Begin
    if StatusBarList.Items[i].ThreadID = ThreadID then
    Begin
      aStatusBar := StatusBarList.Items[i];
      KillThreadList.Extract(ThreadID);
      StatusBarList.Extract(aStatusBar);
      aStatusBar.Free;
      break;
    End;
  End;

  self.Refresh;
end;

In the case above, I am also removing some GUI stuff.

Hope that helps. SpringerRider

Shan answered 18/12, 2013 at 20:57 Comment(1)
" = True" is awesome!Bracelet

© 2022 - 2024 — McMap. All rights reserved.