I've read the MySQL documentation about writing thread-safe client applications under Win32. I've also read several posts where people ask why mysql_thread_init and my_thread_init don't seem to be defined (linker error) when compiling against the LIB/DLL under Win32. I still read many people saying that Win32 programmers still have to call the my(sql)_thread_init functions in each thread. However, upon examining the source code for the Win32 version of libmysql, I do not believe that is the case. Referring to mysql-4.0.14b\libmysql\dll.c, I see the following code for the entrypoint of the libmysql.dll:

Code:
BOOL APIENTRY LibMain(HANDLE hInst,DWORD ul_reason_being_called,
		      LPVOID lpReserved)
{
  switch (ul_reason_being_called) {
  case DLL_PROCESS_ATTACH:	/* case of libentry call in win 3.x */
    if (!inited++)
    {
      s_hModule=hInst;
      libmysql_init();
      main_thread=GetCurrentThreadId();
    }
    break;
  case DLL_THREAD_ATTACH:
    threads++;
    my_thread_init();
    break;
  case DLL_PROCESS_DETACH:	/* case of wep call in win 3.x */
     if (!--inited)		/* Safety */
     {
       /* my_thread_init() */	/* This may give extra safety */
       my_end(0);
     }
    break;
  case DLL_THREAD_DETACH:
    /* Main thread will free by my_end() */
    threads--;
    if (main_thread != GetCurrentThreadId())
      my_thread_end();
    break;
  default:
    break;
  } /* switch */

  return TRUE;

  UNREFERENCED_PARAMETER(lpReserved);
} /* LibMain */
According to this code, the libmysql DLL itself should be notified when it is attached to a new process, and call the libmysql_init function (which in turn calls my_init), and the DLL is also notified when it is attached to each new thread created, causing it to call my_thread_init. The cleanup function calls to my_thread_end and my_end are also handled by the DLL's notification messages from Windows. Because of this it seems inadvisable to call any of the 4 the functions again yourself.

Unfortunately, as much as I have searched, I have found no reference to this difference in the Win32 version of libmysql in any documentation or forums posts.

Can anyone confirm or contradict this? Is this documented somewhere?

_____________________

Jeremiah Gowdy
Senior Software Engineer
FreedomVOICE Systems
_____________________