標籤:
1. Building the library as a statically linkable library
-----------------------------------------------------
General: PTW32_STATIC_LIB must be defined for both the library build and the
application build. The makefiles supplied and used by the following ‘make‘
command lines will define this for you.
MSVC (creates pthreadVCn.lib as a static link lib):
nmake clean VC-static
MinGW32 (creates libpthreadGCn.a as a static link lib):
make clean GC-static
Define PTW32_STATIC_LIB when building your application. Also, your
application must call a two non-portable routines to initialise the
some state on startup and cleanup before exit. One other routine needs
to be called to cleanup after any Win32 threads have called POSIX API
routines. See README.NONPORTABLE or the html reference manual pages for
details on these routines:
BOOL pthread_win32_process_attach_np (void);
BOOL pthread_win32_process_detach_np (void);
BOOL pthread_win32_thread_attach_np (void); // Currently a no-op
BOOL pthread_win32_thread_detach_np (void);
The tests makefiles have the same targets but only check that the
static library is statically linkable. They don‘t run the full
testsuite. To run the full testsuite, build the dlls and run the
dll test targets.
=============================================
2. 使用pthread-win32靜態庫
使用pthread-win32靜態庫要注意:
1) 在程式中要定義宏PTW32_STATIC_LIB
2) pthread-win32靜態庫中沒有做attach和detach操作,要程式員來完成該任務,
否則線程有能成功建立。具體如下:
在程式入口(通常是main函數)處調用如下兩個函數
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
程式結束時調用
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
沒辦法,windows系統有很多標準它都不支援,寫起來就要麻煩點。
可以簡單地這樣處理:
a) 定義程式結束時的處理函數
#ifdef PTW32_STATIC_LIB
void pthread_win32_detach(void)
{
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
}
#endif
b) 在程式入口處調用如下
#ifdef PTW32_STATIC_LIB
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
atexit(pthread_win32_detach);
#endif
windows下靜態編譯pthread