[fpc-pascal] Forcibly terminating a thread

Jonas Maebe jonas.maebe at elis.ugent.be
Sat Feb 27 13:21:34 CET 2016


On 27/02/16 12:40, Mark Morgan Lloyd wrote:
> Is there a way of sending an unambiguous system-level kill signal to a
> thread? KillThread(ThreadID) doesn't appear to be brutal enough, and I'm
> unsure of the correct way to access pthread_kill().

You cannot asynchronously kill threads on Unix platforms with the 
cthreads unit, and even on platforms where you can, you should never do 
it. All resources held by that thread will leak, and locks it held will 
never be unlocked.

KillThread() on Unix calls pthread_cancel(), which indicates to the 
system libraries (or any other libraries that include support for this) 
that that as soon as they know it is safe to kill the current thread, 
they should abort it and call its registered cleanup functions.

Since you are in an emulator, the way to solve this is probably to have 
your own threads regularly check the "terminated" property of the 
tthread and if it's set (which you can do by calling thread.terminate 
"on it" from another thread), to clean up everything and abort.


Jonas



More information about the fpc-pascal mailing list