Re: Monitor Thread In DLL
From: Pavel Lebedinsky (m_pll)
Date: 12/10/04
- Next message: Pavel A.: "Re: USB configuration"
- Previous message: James Antognini [MSFT]: "Re: [DDK] Which .lib is required?"
- In reply to: Matt: "Monitor Thread In DLL"
- Next in thread: Matt: "Re: Monitor Thread In DLL"
- Reply: Matt: "Re: Monitor Thread In DLL"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 10 Dec 2004 14:09:10 -0800
I would go with a cleanup function. If clients forget to call it
then the thread will be terminated but you should be able
to handle this case anyway (meaning it shouldn't cause
permanent corruption of on-disk data etc), because in the end
nothing prevents the user from killing your process with taskmgr
or kill.exe.
By the way, if you want to be able to unload the DLL
at runtime, take a look at the FreeLibraryAndExitThread
API.
"Matt" wrote:
>
> Hi,
>
> I am creating a monitor thread in a WIN32 DLL. I'd like the monitor
> thread to stay around for the life of the DLL and then exit cleanly before
> the DLL is detached.
>
> My problem is, I do not know how to "safely" do this. I have tried using
> the atexit() function inside the DLL to trigger the monitor thread
> termination, but it is already too late by then. It looks like the C++
> run time calls:
>
> atexit() for the EXE
> ExitProcess() -> Kills all active worker threads using TerminateThread().
> atexit() for each DLL during the detach phase.
>
> I really want to avoid having my worker thread killed by
> TerminateThread(). I could get around this by requiring the user to call
> some type of cleanup function, but I would like to avoid doing that as
> well (because some users may forget to call it).
>
> CleanupStuff(), waits for thread shutdown. Cleans up the thread nicely.
> atexit() for the EXE
> ExitProcess() -> Kills all active worker threads using TerminateThread().
> atexit() for each DLL during the detach phase.
>
> I could try implicitly loading the atexit() function for the executable
> and add in my cleanup code there, but that seems very much like a hack to
> me.
>
> LoadLib for the atexit() func for the EXE. Call it.
> atexit() for the EXE. Calls my cleanup function and waits for thread
> shutdown. Cleans up the thread nicely.
> ExitProcess() -> Kills all active worker threads using TerminateThread().
> atexit() for each DLL during the detach phase.
- Next message: Pavel A.: "Re: USB configuration"
- Previous message: James Antognini [MSFT]: "Re: [DDK] Which .lib is required?"
- In reply to: Matt: "Monitor Thread In DLL"
- Next in thread: Matt: "Re: Monitor Thread In DLL"
- Reply: Matt: "Re: Monitor Thread In DLL"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|