pthreads-w32
http://shenan1984.blog.163.com/blog/static/2530851020098231001787/official site: http://sourceware.org/pthreads-win32/.
source co
1. 編譯:
雖然源碼包里提供了vc6的項目文件, 但是打不開的, 只能用nmake. 默認的會告訴你一堆nmake參數的.
我所要用的是編譯成static的library, 所以輸入"nmake clean VC-static", 編譯很快的. 不過默認會鏈接到VC的crt, 我們需要修改它的makefile. 找到CFLAGS那一行, 把"/MD"改成"/MT".
2. 項目:
誒.. 有好多地方要改的.
a) 當然是vs路徑的include啊, lib啊.. 自己加.
b) 項目的crt設置成"/MT"和"/MTd". 額外的lib加: pthreadVC2(d).lib ws2_32.lib
c) preprocesser定義的地方, 加一個“PTW32_STATIC_LIB”宏, 不然link的時候會找不到symbol的.
d) 好了, 你可以coding了, 隨便pthread_create()一把吧.
3. 編碼:
嗯嗯.. 如果真的直接pthread_create()的話可是會access violation的呀. win32下的線程很詭異的, 像winsock一樣, 調用任何其它函數之前必須調用pthread_win32_process_attach_np(), 結束后必須調用pthread_win32_process_detach_np(). 代碼大概就是這樣的:
1. 下載pthreads win32源代碼:
ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.tar.gz
2. 編譯靜態庫:
make clean GC-static
在根目錄下面生成libpthreadGC2.a
3. 將生成的libpthreadGC2.a拷貝到mingw庫目錄下,將pthread.h, sched.h, semaphore.h拷貝到INCLUDE目錄下
4. 使用libpthread庫,
在程序起始處對libpthread作初始化:
#if defined(PTW32_STATIC_LIB)
ptw32_processInitialize();
#endif
5. 編譯時確保傳入-DPTW32_STATIC_LIB,鏈接時加入-lpthreadGC2, OK!
http://hi.baidu.com/lff0305/blog/item/2a55e7366ebba6360b55a942.html
pthread2提供了不同的.lib以及相對應的.dll,
pthread[VG]{SE,CE,C}c.dll
pthread[VG]{SE,CE,C}c.lib
含義為
[VG] 編譯器種類
V - MS VC, or
G - GNU C
{SE,CE,C} 異常處理模式
SE - Structured EH, or
CE - C++ EH, or
C - no exceptions - uses setjmp/longjmp
c - DLL compatibility number indicating ABI and API
compatibility with applications built against
any snapshot with the same compatibility number.
See 'Version numbering' below.
比如上面用的pthreadvc2.dll, 含義為:
v = MSVC
c = 沒有使用異常機制, 而是使用setjump/longjmp
2 = 兼用性 - 和pthread2兼容, 不和舊版本pthread1兼容.
詳細請參照pthread的readme.
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.
posted on 2010-11-15 15:41 八葉草 閱讀(2296) 評論(1) 編輯 收藏 引用 所屬分類: pthreads-w32

