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.