[fpc-pascal] Forcibly terminating a thread

Mark Morgan Lloyd markMLl.fpc-pascal at telemetry.co.uk
Sat Feb 27 15:15:14 CET 2016


Jonas Maebe wrote:
> 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.

Ouch. But as I said, this is specifically at termination, I'd like to 
shut down in good order so that I can output the number of times each 
thread's spun and none of them will be reused.

> 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.

It's not so much /in/ an emulator as the code /is/ an emulator.

The threads check for various termination conditions every time they 
spin, but in this test case one of them's gone into a syscall that can't 
be completed.

Interestingly, this is /exactly/ why a dot was added after the final END 
in a number of ALGOL dialects: the emulator is for a B5500 and the 
hardware doesn't have a "no more cards in hopper" signal.

"The first thing that Dijkstra wanted to see was BEGIN END.
compiled and executed.

"We apologized for the “.” being required since that was not a part of 
Algol. The “.” did not bother him at all. He thought it was a good idea."

And that's obviously why we've got it in Pascal as well.

-- 
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]



More information about the fpc-pascal mailing list