@dbannon
You actually made some meaning for the loop, but I think this is an even worse solution than the one I have. You use a 100ms Sleep (a sleep I wanted to avoid at the first place) and if the flag is set, within 100ms it is noticed and acted on in the main loop. It means that your process must be running every 100ms and You cannot make the sleep much longer, because then the signal is handled with a long delay. Not a big deal, to run the CPU for few clock cycles every 100ms, but if you do the tiding up directly in the SignalRec, then it is done immediately (not somewhere within the length of the sleep) and so the sleep can be much-much longer.
@MarkMLI
I don't really understand how your solution worked (and what was wrong in it).
@440bx
If I can trust SO, it is not implemented in Linux:
https://stackoverflow.com/questions/11468333/linux-threads-suspend-resumeAlso, IIRC in Windows that was for a thread other than the main program. I think the main program cannot stop itself.
In the meantime, I was also thinking another solution, but in this form, it does not work:
var
Access : TRTLCriticalSection;
begin
InitCriticalSection(Access);
EnterCriticalSection(Access);
EnterCriticalSection(Access);
writeln('Finished');
end.
I thought that entering a critical section checks if that semaphore is already blocked, but apparently (as it is correctly written in the manual as well) it checks if that is already blocked BY ANOTHER thread. I probably still could use this trick, first entering a critical section in a sub-thread (and never leaving it) and then try to enter it in the main program as well. This would have the problem though that if for whatever reason the sub-thread is slow, the main program might get there first and exit. With some more checks I can still solve this.
Can it be a working and efficient solution?