locked
Mutex Locking in WinRT C++

    Question

  • I've been trying every permutation and combination I can think of but I can't figure out how to create a Mutex object in C++ for a Universal application.  Does anyone have an extremely simple code snippet that has the proper include/namespace to create a Mutex? 

    Thank you very much!
    Monday, January 12, 2015 11:21 PM

Answers

  • System.Threading is a .Net namespace and can only be used in .Net applications.

    Windows Runtime apps can use the parts of the Win32 and COM APIs that are documented for use in Windows Store apps. These are listed in the Win32 and COM APIs section in the Windows Runtime API Reference docs and in the requirements section on the pages for the individual API docs.

    If you look at the bottom of the CreateMutexEx page it lists Windows Store apps:

    Minimum supported client

    Windows Vista [desktop apps | Windows Store apps]

    C++/Cx is native C++ with a some extensions to make it easier to call Windows Runtime classes. Since it's C++ it can use the C++ standard library, including std::mutex.

    Tuesday, January 13, 2015 3:24 PM
    Owner

All replies

  • I assume you want to protect shared resource you could use Critical_section instead

    Concurrency::critical_section mCriticalSeciton;

    then lock with

    critical_section::scoped_lock lock(mCriticalSeciton);

    Tuesday, January 13, 2015 3:30 AM
  • Have you tried using std::mutex?
    Tuesday, January 13, 2015 5:40 AM
  • 'standard has no member mutex'. 

    It's entirely possible I'm just not including the right header file.   But I'm growing very suspicious that since the MSDN page doesn't include a C++ example that it's exclusively a C# feature now.  Which strikes me as incredibly odd that the managed language would include such a low level threading tool but not C++. 
    Tuesday, January 13, 2015 6:31 AM
  • Which MSDN page are you looking at? If you're looking at the System.Threading.Mutex from .Net then it can be called from .Net languages such as C# but not from unmanaged languages such as C++/Cx.

    From native code you can call Win32's CreateMutexEx, which is supported in both Windows Store and desktop apps. #include <windows.h> to use it. See Using Mutex Objects for an overview (desktop app oriented so not completely applicable, but it will give the general idea).

    As Jim suggested, from C++ you can use std::mutex . You will need to #include <mutex>

    Tuesday, January 13, 2015 7:37 AM
    Owner
  • Thanks I'll give that a try tomorrow. I figured since it was win32 it would violate store checks.

    http://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx

    This was the article I was reading previously.

    Tuesday, January 13, 2015 7:42 AM
  • System.Threading is a .Net namespace and can only be used in .Net applications.

    Windows Runtime apps can use the parts of the Win32 and COM APIs that are documented for use in Windows Store apps. These are listed in the Win32 and COM APIs section in the Windows Runtime API Reference docs and in the requirements section on the pages for the individual API docs.

    If you look at the bottom of the CreateMutexEx page it lists Windows Store apps:

    Minimum supported client

    Windows Vista [desktop apps | Windows Store apps]

    C++/Cx is native C++ with a some extensions to make it easier to call Windows Runtime classes. Since it's C++ it can use the C++ standard library, including std::mutex.

    Tuesday, January 13, 2015 3:24 PM
    Owner