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

在Windows上使用静态编译的pthread库时,需要显式调用pthread_win32_process_attach_np、pthread_win32_thread_attach_np、pthread_win32_thread_detach_np和pthread_win32_process_detach_np函数,以确保线程库的正确初始化和清理。这些函数对应dllMain的功能,必须在程序开始和结束时调用,以避免API调用崩溃。通常在DLLMain中处理这些attach和detach操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

作为通用的跨平台高性能线程库,在很多跨平台的项目中都可以看见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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值