How to make C ++ scoped_lock or C # lock (), which works for both C # and C ++ in WinRT

I am creating a program that has a C ++ / CX WinRT base library with a C # / XAML application level. I need to do some kind of mutex / critical_section / lock () to prevent the appearance of multiple threads from the typical multi-threaded insanity on some resources of the class defined in the C ++ library.

Please note that both sides of C ++ and C # can access resources that need to be blocked, so I need some kind of interface that works for both.

The C ++ code I started working with (WinRT DirectX sample) uses a critical_segment, but it cannot be directly exported and cannot scoped_lock, because they are not compatible with WinRT (i.e. ref and are sealed)

I created a simple C ++ / CX class that worked like scoped_lock (i.e. locking in the constructor, unlocking in the destructor), but considering unpredictability, when the destructor is called in C #, this does not work for obvious reasons.

Now I only have the manual Lock () and Unlock () methods in the class that contains the resources, then it calls lock () or unlock () for the critical_section object, and it works, but it lacks the elegance and clarity of either the scoped_lock class. either the C # lock keyword.

Ideally, I would prefer to use the C # lock keyword to lock the class (or lock some member of the class), but my search-fu does not show me any examples of how to configure the C ++ class to also lock the same object.

+4

All Articles