Answered by:

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.

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++.

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>

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.