Every Windows DLL can provide a DllMain function. This function gets called
whenever a thread gets created or destroyed.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dllmain.asp


Some example code:

/**
\brief
*/
BOOL WINAPI DllMain(
   HINSTANCE  hinstDLL,
   DWORD      fdwReason,
   LPVOID     /*lpReserved*/)
{
    // Perform actions based on the reason for calling.
    switch( fdwReason )
    {
        case DLL_PROCESS_ATTACH:
           // Initialize once for each new process.
           // Return FALSE to fail DLL load.
           myDllHandle = hinstDLL;

           // Allocate a TLS index.
           if((myTlsIndex = TlsAlloc()) == 0xFFFFFFFF)
              return FALSE;

            break;

        case DLL_THREAD_ATTACH:
            // Do thread-specific initialization.
            break;

        case DLL_THREAD_DETACH:
            // Do thread-specific cleanup.
            // Release the allocated memory for this thread.
            myFreeUserMessageBufferOfThread();

            break;

        case DLL_PROCESS_DETACH:
           NI_DEBUG_ASSERT(myRefCount == 0); // usi not correctly
deinitialized
           myFreeUserMessageBufferOfThread();

           TlsFree(myTlsIndex);
           myTlsIndex = myTlsIndexInvalid;

           break;
    }
    return TRUE;  // Successful DLL_PROCESS_ATTACH.
}




                                                                           
             [EMAIL PROTECTED]                                                 
                                                                           
             11.01.2006 16:00                                           To 
                                       sqlite-users@sqlite.org             
                                                                        cc 
             Please respond to                                             
             [EMAIL PROTECTED]                                     Subject 
                  te.org               [sqlite] Thread handling in Windows 
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




In the unix world using pthreads, when you allocate
thread-local storage, you specify a callback function
to delete the storage when the thread terminates.
This callback is the second argument to
pthread_key_create(),  See, for example,

  http://www.mkssoftware.com/docs/man3/pthread_key_create.3.asp

Question:  How do you do the same thing on windows?
How do you get a thread to clean up its thread-local-storage
obtained using TlsAlloc() and TlsSetValue() when the
thread terminates?

I need an answer to this question so that I can fix
ticket #1601:  http://www.sqlite.org/cvstrac/tktview?tn=1601

--
D. Richard Hipp <[EMAIL PROTECTED]>



Reply via email to