windows - Hooking thread creation/termination -


Is it possible to hook up thread ending on Windows? IOW, I have to inform that inside the process a thread (not interested in other processes and their threads) has ended (either in general or - more important - forcibly).

Alternatively, threads will also be connected in the production. Logic: I have a library that manages some information on the basis of per-thread (it takes some detailed information as a process-wide per-thread cache). When a thread ends, I will need to remove all thread-specific information from the cache. [Cash associations are implemented using the thread id, which can be reused for future threads.]

There is no problem with the "normal" execution order because the Library user current The thread will separate the library from the library which will clear the state. If a person kills the resources that are cached, the problems start to appear.

You can use some Win32 APIs like hooking up API-level.

I do not see why you need to do this, though. It seems that the associated cache of the thread needs to be cleaned, when the thread dies, then you can reuse that slot, if the second thread with the same ID comes along. Is it true?

If so, can not you clear the Cash Association in DllMain when you receive the DLL_THREAD_ATTACH event? This is essentially your new thread notification. At this point, you know that you have a new thread, so is it not safe to clear the existing affiliate cache?

The second option that can work (TLS) You can use the Win32 API to store / run thread-specific information. You can also define a variable so that the compiler can manage TLS for you. In this way, each thread keeps its cache, the code remains the same for each thread, but the data is relative to the access thread.


Comments