在 Windows 下用 TDM-GCC(MinGW)开发 DLL 涉及到数据同步锁及 DLL 初始化终止化函数的问题

在 Windows 下用 TDM-GCC(MinGW)开发 DLL 如果要用到数据同步锁,理论上可以采用 Windows API 提供的临界区实现(需要用到的函数有 InitializeCriticalSection、DeleteCriticalSection、EnterCriticalSection、LeaveCriticalSection),也可以采用 GCC 的 pthread 库中的 pthread mutex lock 互斥锁来实现(需要用到的函数有 pthread_mutex_init、pthread_mutex_destroy、pthread_mutex_lock、pthread_mutex_unlock)。但是在实际开发过程中发现,用 Windows API 的临界区实现数据同步,会导致 DLL 运行失败,目前原因还不明。无奈之下,改用 pthread 互斥锁实现数据同步。pthread 本身是跨平台的,编译链接时,使用 -lpthread 参数,预绑定到 pthread 的动态库,测试发现 DLL 运行良好。后来用 objdump 分析生成的 DLL,发现使用 pthread 互斥锁时,DLL 并没有真正引用 pthread 的库以及有关函数,所以猜测可能 pthread 针对 Windows 平台是采用 Windows API 提供的其他机制实现了互斥锁效果吧。

另外发现一点,TDM-GCC(MinGW)开发 DLL 时,仍然可以使用 GCC 特有的动态库(共享库)初始化、终止化函数机制,比如下面这样:

 void __attribute__((constructor)) dynamic_library_init() {
pthread_mutex_init(&_ptm_mutex, NULL);
printf("dll _init() called OK\n");
} void __attribute__((destructor)) dynamic_library_fini() {
pthread_mutex_destroy(&_ptm_mutex);
printf("dll _fini() called OK\n");
}

上面的 dynamic_library_init() 和 dynamic_library_fini() 是我自定义的函数名,你可以改为其他名字。因为默认的 _init() 和 _fini() 这两个初始化、终止化函数已经 GCC 占用,我们无法使用,所以必须通过 GCC 扩展机制定义自己的动态库、共享库初始化终止化函数。这样就可以避免在 Windows 下使用 DLLMain 机制,实现源代码级跨平台支持,将需要在 DLL 载入初始化或卸载终止化时执行的重要操作放在这两个函数里面。

上一篇:git 提交代码到github错误处理


下一篇:[tools]tcp/udp连通性测试