Today I ran into a problem with an application I originally programmed under Windows:
A thread should be suspended by another thread. The Unix implementation (
tthread.inc) of FPC throws me an error here:
Suspending one thread from inside another one is unsupported (because it is unsafe and deadlock prone) by *nix and posix operating systems
The
comment of the person who wrote this implementation for FPC says:
Posix doesn't define anything for suspending threads as this is
inherintly unsafe. Just don't suspend threads at points they cannot
control. Therefore, I didn't implement .Suspend() if its called from
outside the threads execution flow (except on Linux _without_ NPTL).
My question is now: How do I properly tell one thread to wait, until another thread tells it to resume?
Usually the answer I found, and would use myself, are
RTLEvents.
Edit: I found the actual solution myself after some more testing
I created a "Pause" method on my thread to set a boolean flag via InterlockedExchange to switch it by any outside thread. Then in my thread's main loop I check if this flag is set. If yes, I use RTLWaitFor. Then I created a "Continue" method which sets the RTLEvent for this, to resume (and of course sets the flag to false).