- Open (single-click) the built .exe in VSCode and press F5
- study how to replace dangling mutex belongs to crashed / terminated process by new mutex
- c++ - interprocess::named_upgradable_mutex - remains locked if process is killed - Stack Overflow
- UPDATE
atexit
in normal termination (Ctrl + C) can clean up mutex- Signal handlers may also work for normal termination
- Anyway, process killing cannot trigger any cleaning, dangling mutex still there
- UPDATE 2023
-
OS can tell us if a system mutex is dangling:
-
- Obtain a handle to the mutex: If you know the name of the mutex, you can use the
OpenMutex
function to obtain a handle to it. This function takes the desired access rights and the name of the mutex as parameters and returns a handle to the mutex if it exists.
- Obtain a handle to the mutex: If you know the name of the mutex, you can use the
HANDLE hMutex = OpenMutex(SYNCHRONIZE, FALSE, L"MutexName");
-
- Check if the handle is valid: The
OpenMutex
function will return a valid handle if the mutex exists and you have the necessary access rights. You can check if the handle is valid by comparing it to theNULL
value.
- Check if the handle is valid: The
if (hMutex != NULL) { // Mutex exists and handle is valid // The mutex is still active } else { // Mutex does not exist or handle is invalid // The mutex is not active }
-
- Close the handle: After you have finished checking the mutex's status, it's important to close the handle using the
CloseHandle
function to release system resources.
- Close the handle: After you have finished checking the mutex's status, it's important to close the handle using the
CloseHandle(hMutex);
-
-
- UPDATE 2024
- don't trust chatgpt code above
- C++多线程之使用Mutex和Critical_Section_std::mutex ccriticalsection-CSDN博客
- query mutex state function can be non-waiting
- 使用 Mutex 物件 - Win32 apps | Microsoft Learn
- implemented and working
- this article tells everything
- We can query mutex state
By following these steps, you can determine if a mutex is still active on Windows using the Win32 API. Remember to handle errors appropriately and ensure that you have the necessary access rights to open the mutex.
- compare boost mutex to std::mutex