Skip to content

dirkarnez/cpp-mutex-playground

Repository files navigation

cpp-mutex-playground

To debug in VSCode

  • Open (single-click) the built .exe in VSCode and press F5

TODOs

  • 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:

          1. 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.
        HANDLE hMutex = OpenMutex(SYNCHRONIZE, FALSE, L"MutexName");
          1. 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 the NULL value.
        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
        }
          1. 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.
        CloseHandle(hMutex);
    • UPDATE 2024

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

Reference

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published