Using TDM-GCC (MinGW) in Windows to develop DLL involves the problem of data synchronization lock and DLL initialization termination function, tdm-gccmingw

Source: Internet
Author: User
Tags pthread mutex

Using TDM-GCC (MinGW) in Windows to develop DLL involves the problem of data synchronization lock and DLL initialization termination function, tdm-gccmingw

In Windows with TDM-GCC (MinGW) Development DLL if you want to use data synchronization lock, theoretically, you can use the critical section provided by Windows APIs (required functions include InitializeCriticalSection, DeleteCriticalSection, EnterCriticalSection, and LeaveCriticalSection ), you can also use the pthread mutex lock in the pthread library of GCC to implement the mutex lock (the required functions include pthread_mutex_init, pthread_mutex_destroy, pthread_mutex_lock, and pthread_mutex_unlock ). However, during actual development, we found that using the Windows API critical section for Data Synchronization may cause DLL operation failure. The reason is unknown. Instead, use the pthread mutex lock to synchronize data. Pthread itself is cross-platform. When compiling a link, use the-lpthread parameter to pre-bind it to the dynamic library of pthread. The test shows that the DLL runs well. Later, I used objdump to analyze the generated DLL and found that when pthread is used for mutex lock, the DLL did not actually reference the pthread library and related functions, so I guess pthread may use other mechanisms provided by Windows API to implement mutex lock on Windows platforms.

 

Also found that TDM-GCC (MinGW) DLL development, you can still use the dynamic library (shared library) GCC-specific initialization, termination function mechanism, such as the following:

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

The above dynamic_library_init () and dynamic_library_fini () are my custom function names. You can change them to other names. Because the default _ init () and _ fini () initialization and termination functions are occupied by GCC, we cannot use them, therefore, you must use the GCC extension mechanism to define your own dynamic library and shared library initialization termination functions. In this way, you can avoid using the DLLMain mechanism in Windows to implement source code-level cross-platform support, and put the important operations that need to be performed during DLL loading initialization or uninstallation termination into these two functions.


Related Article

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.