[fpc-pascal] Question: Is there a global callback, when a thread gets terminated?
Martin
fpc at mfriebe.de
Fri Feb 1 16:47:01 CET 2019
Lets say you have a unit, with code that can be called from threads.
- The code does not own the thread object, so it can *not* use
TThread.OnTerminate (assuming it could get hold of the thread object).
- Yet the code wants to use "threadvar" and create objects (or otherwise
allocate mem). The code should return the memory, when the thread
terminates.
- The user of the code should not need to make any calls, to initiate
the clean up.
So that would need something like
GetCurrentThreadObject.AddThreadTermationHandler(MyHandler);
or
GlobalThreadTerminationHandler.Add(MyHandler);
MyHandler does not need the thread-id, or -object. But it needs to run
in the correct context for the ThreadVar. That is it needs to see the
value the threadvar has for the thread that is terminating.
So the Handler could do something like "myThreadVar.Free".
Any ideas?
I know the memory manager gets called "MemoryManager.DoneThread".
It is possible to intercept this, by getting the current mem-mgr,
replacing the method with a wrapper method (calling the original
method), and setting the mem-mgr.... Not particular nice.
More information about the fpc-pascal
mailing list