• 在 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 特有的动态库(共享库)初始化、终止化函数机制,比如下面这样:

    1 void __attribute__((constructor)) dynamic_library_init() {
    2     pthread_mutex_init(&_ptm_mutex, NULL);
    3     printf("dll _init() called OK
    ");
    4 }
    5 
    6 void __attribute__((destructor)) dynamic_library_fini() {
    7     pthread_mutex_destroy(&_ptm_mutex);
    8     printf("dll _fini() called OK
    ");
    9 }

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

  • 相关阅读:
    如何解决Pulling without specifying how to reconcile divergent branches
    Mac设置终端打开快捷键
    Mac 息屏快捷键
    Windows安装使用Openssl
    tomcat证书转换成nginx证书。jks/keystore > crt/key
    Windows下类似Linux的CAT命令是什么
    齐文词根词缀---3.23、co-(放在元音前面)表示共同,(和com和con一个意思)
    齐文词根词缀---3.22、clus-关闭(就是close)
    齐文词根词缀---3.21、clam/claim-喊
    齐文词根词缀---3.20、cis-切、割(同cid)
  • 原文地址:https://www.cnblogs.com/cyberniuniu/p/5021957.html
Copyright © 2020-2023  润新知