pthreads-w32
http://shenan1984.blog.163.com/blog/static/2530851020098231001787/official site: http://sourceware.org/pthreads-win32/.
source co de: ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.tar.gz
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.