http://blog.csdn.net/psusong/archive/2010/01/14/5189659.aspx
pthread 靜態編譯版本在Windows下使用時的注意事項
作為通用的跨平臺高性能線程庫,在很多跨平臺的項目中都可以看見pthread的身影。pthread本身的實現比較優雅,APIs使用起來也很方便。
但在Windows下使用靜態編譯的pthread時要特別注意一下,必須顯式的調用如下四個函數,否則pthread用到的一些全局變量會沒有被初始化,導致所有的pthread的APIs調用都crash.
BOOL pthread_win32_process_attach_np (void);
BOOL pthread_win32_process_detach_np (void);
BOOL pthread_win32_thread_attach_np (void);
BOOL pthread_win32_thread_detach_np (void);
pthread官方文檔對此有如下的明確說明:
These functions contain the code normally run via dllMain
when the library is used as a dll but which need to be
called explicitly by an application when the library
is statically linked.
You will need to call pthread_win32_process_attach_np() before
you can call any pthread routines when statically linking.
You should call pthread_win32_process_detach_np() before
exiting your application to clean up.
pthread_win32_thread_attach_np() is currently a no-op, but
pthread_win32_thread_detach_np() is needed to clean up
the implicit pthread handle that is allocated to a Win32 thread if
it calls certain pthreads routines. Call this routine when the
Win32 thread exits.
These functions invariably return TRUE except for
pthread_win32_process_attach_np() which will return FALSE
if pthreads-win32 initialisation fails.
通過函數的名字我們不難猜測出如下調用順序
在程序開始的時候要調用:
BOOL pthread_win32_process_attach_np (void);
BOOL pthread_win32_thread_attach_np (void);
在程序退出時要調用:
BOOL pthread_win32_thread_detach_np (void);
BOOL pthread_win32_process_detach_np (void);
比較通用的做法是在模塊Load和UnLoad的時候做這個attach和detach操作,如下面所示:
/* Callback for our DLL so we can initialize pthread */
BOOL WINAPI DllMain( HANDLE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
{
#ifdef PTW32_STATIC_LIB
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
pthread_win32_process_attach_np();
case DLL_THREAD_ATTACH:
pthread_win32_thread_attach_np();
break;
case DLL_THREAD_DETACH:
pthread_win32_thread_detach_np();
break;
case DLL_PROCESS_DETACH:
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
break;
}
#endif
return TRUE;
}
注意: PTW32_STATIC_LIB 宏為pthread靜態編譯的標志,這個可以通過pthread.h的配置或者CFLAGS傳遞進來。
下面是pthread的官方的dll.c的實現
BOOL WINAPI
DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
{
BOOL result = PTW32_TRUE;
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
result = pthread_win32_process_attach_np ();
break;
case DLL_THREAD_ATTACH:
/*
* A thread is being created
*/
result = pthread_win32_thread_attach_np ();
break;
case DLL_THREAD_DETACH:
/*
* A thread is exiting cleanly
*/
result = pthread_win32_thread_detach_np ();
break;
case DLL_PROCESS_DETACH:
(void) pthread_win32_thread_detach_np ();
result = pthread_win32_process_detach_np ();
break;
}
return (result);
} /* DllMain */
也就是說pthread官方代碼在動態編譯的版本中主動做了這個attach和detach操作。
而靜態編譯版本由于沒有一個合適的地方來做這件事,就將attach和detach的的操作扔給用戶來完成了。
上面的代碼是針對調用方是Dll的情況做的初始化,如果調用方不是Dll呢?對此可以參照如下做法,雖然很暴力,但很簡單,可以工作
1)定義如下函數
#ifdef PTW32_STATIC_LIB
static void detach_ptw32(void)
{
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
}
#endif
2)在你的主程序的入口處,一般而言是main()中做如下調用即可
#ifdef PTW32_STATIC_LIB
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
atexit(detach_ptw32);
#endif
也就是用atexit()將detach工作掛接到程序中去,使得程序在退出的時候可以對pthread進行detach.