Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-09

is it possible to kill a thread from within fpc?

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dennis Poon

I want to kill a thread created by my fpc program (not killing the entire process).
I have to use an external win32 dll.
I am already calling it from a separate thread but still, that dll something goes into an infinite loop which made my thread freezes.

How do I kill this particular thread? Just by calling MyThread.terminate definitely just does not work.

Is it even possible?

Dennis

Jonas Maebe
to Dennis Poon
Dennis Poon wrote 10 lines
I want to kill a thread created by my fpc program (not killing the entire process). I have to use an external win32 dll. I am already calling it from a separate thread but still, that dll something goes into an infinite loop which made my thread freezes. How do I kill this particular thread? Just by calling MyThread.terminate definitely just does not work. Is it even possible?

There is no FPC-supplied functionality for this. In general, killing threads from an external thread is something you should never do, because you have no idea what kind of resources this may leave dangling. Terminating another thread at any point was available in early Java versions, and was removed later. It was also available in old Delphi versions and was removed later. Just don't do it.

Calling "terminate" sets the "terminated" property to "true", which you can check in the run loop in the thread and make it exit itself at this (presumably safe) point.

Jonas

Mark Morgan Lloyd
to Dennis Poon: I want to kill a thread created by my fpc program (not killing the entire process). I…
Dennis Poon wrote 5 lines
I want to kill a thread created by my fpc program (not killing the entire process). I have to use an external win32 dll. I am already calling it from a separate thread but still, that dll something goes into an infinite loop which made my thread freezes.

Are you absolutely sure that the loop is a coding error in the DLL, or is there any possibility that it's either because you're not calling it properly, because you've not initialised something properly, or because you're mistakenly allocating memory using a heap manager in the DLL and then releasing it in the main program or vice-versa?

Noting everything that Jonas has said, and pointing out that there's been extensive discussion in the past relating to even calling Suspend from inside the thread's own code, I'd point out that it's fairly common practice for programs such as Firefox to launch a separate process (not thread) to handle e.g. an instance of the Flash player. Is this something you could do, or do you have to have fine-grained control of multiple subroutines in the DLL?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Dennis Poon
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 21 lines
Dennis Poon wrote: > I want to kill a thread created by my fpc program (not killing the > entire process). > I have to use an external win32 dll. > I am already calling it from a separate thread but still, that dll > something goes into an infinite loop which made my thread freezes. Are you absolutely sure that the loop is a coding error in the DLL, or is there any possibility that it's either because you're not calling it properly, because you've not initialised something properly, or because you're mistakenly allocating memory using a heap manager in the DLL and then releasing it in the main program or vice-versa? Noting everything that Jonas has said, and pointing out that there's been extensive discussion in the past relating to even calling Suspend from inside the thread's own code, I'd point out that it's fairly common practice for programs such as Firefox to launch a separate process (not thread) to handle e.g. an instance of the Flash player. Is this something you could do, or do you have to have fine-grained control of multiple subroutines in the DLL?

It am quite sure it is the dll's problem because the problem happens at 11pm everyday only. Nothing in my program has anything related to that time. The dll is a brokerage firm's order placing system. 11pm is when the market closes so probably the dll has something done at that time that loops forever.
Using a separate process to call it is possible but exchanging data between my program and that process will slow down entire communication and time sensitivity is required for online trading program. The supplier of the dll is quite irresponsive to questions or bug report. Anyway, thanks for all your comments.

Dennis