您的位置:首页 > 其它

pthread 静态编译版本在Windows下使用时的注意事项

2010-01-14 13:56 507 查看
作为通用的跨平台高性能线程库,在很多跨平台的项目中都可以看见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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: