Using TDM-GCC (MinGW) to develop DLLs under Windows involves data synchronization locks and DLL initialization termination functions

Source: Internet
Author: User
Tags pthread mutex

Using TDM-GCC (MinGW) to develop a DLL under Windows if you want to use a data sync lock, you can theoretically use the critical section implementation provided by the Windows API (the function that needs to be used is initializecriticalsection, DeleteCriticalSection, EnterCriticalSection, leavecriticalsection) can also use Pthread mutex lock in the Pthread library of GCC Mutexes are implemented (functions that need to be used are pthread_mutex_init, Pthread_mutex_destroy, Pthread_mutex_lock, Pthread_mutex_unlock). However, in the actual development process, it is found that using the critical section of Windows API to synchronize data, the DLL will fail to run, the reason is unclear. Helpless, the use of pthread mutex to achieve data synchronization. Pthread itself is cross-platform, when compiling links, using-lpthread parameters, pre-binding to Pthread dynamic libraries, Test finds DLLs run well. Later, using Objdump to parse the generated DLLs, the DLL did not really refer to Pthread's libraries and related functions when the pthread mutex was used, so speculation might be pthread for the Windows platform with other mechanisms provided by the Windows API The mutex effect.

It is also found that TDM-GCC (MinGW) can still use GCC-specific dynamic libraries (shared libraries) to initialize and terminate function mechanisms when developing DLLs, such as the following:

1 void__attribute__ ((constructor)) Dynamic_library_init () {2Pthread_mutex_init (&_ptm_mutex, NULL);3printf"DLL _init () called ok\n");4 }5 6 void__attribute__ ((destructor)) Dynamic_library_fini () {7Pthread_mutex_destroy (&_ptm_mutex);8printf"DLL _fini () called ok\n");9}

The above Dynamic_library_init () and Dynamic_library_fini () are my custom function names and you can change them to other names. Because the default _init () and _fini () initialization and termination functions are already gcc-occupied, we cannot use them, so we must define our own dynamic libraries, the shared library initialization termination function through the GCC extension mechanism. This avoids using the DLLMain mechanism under Windows to implement source-level cross-platform support, which will require important operations to be performed during DLL onboarding initialization or unload termination.

Using TDM-GCC (MinGW) to develop DLLs under Windows involves data synchronization locks and DLL initialization termination functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.