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