1

I have a C++ library with managed C++ classes and unmanaged C++ classes, so the library is compiled with /clr support. I need to make some thread safe locking on the unmanaged side but if I include I have the compiler error:

C1189 #error: <mutex> is not supported when compiling with /clr or /clr:pure

How can I work around this? Spent a couple of hours searching but only found very old information. Using Visual Studio 2017 and C++11 language standard.

dxiv
  • 16,984
  • 2
  • 27
  • 49
Pedro Ferreira
  • 852
  • 9
  • 23
  • *"... on the unmanaged side"* - not according to the compiler switches you're using. You're compiling *managed* C++ code if you're compiling with /clr, whether you wish to or not. Your best bet would be to separate the unmanaged code from the managed into a separate DLL project, then either pinvoke or an rcw com-wrap to consume it. Alternatively, embrace the clr you're already using and just use the system threading monitor offerings. – WhozCraig Apr 10 '21 at 21:33
  • Can't use the System::Threading::Monitor because it needs a common managed variable to lock into, and I can't declare a managed attribute in an unmanaged class. – Pedro Ferreira Apr 10 '21 at 21:44
  • There's also the [lock](https://learn.microsoft.com/en-us/cpp/dotnet/lock?view=msvc-160) class. But with /clr flag you really have full managed mode support, and can use managed attributes in "unmanaged" code. – rustyx Apr 10 '21 at 21:59
  • The lock class also needs a managed variable to lock into, and I can't have it in my class if its unmanaged. – Pedro Ferreira Apr 10 '21 at 22:07
  • I'm using un unmanaged class because I'm calling a C++ unmanaged API and I thought it would have better performance. But maybe its nonsense because as you said, the code is already kind of clr. So probably the best solution is to use a managed class directly and use the lock clr class. – Pedro Ferreira Apr 10 '21 at 22:12
  • Duplicates: [one](https://stackoverflow.com/questions/15821942/how-to-implement-a-unmanaged-thread-safe-collection-when-i-get-this-error-mute), [two](https://stackoverflow.com/questions/5670248/boost-mutex-c-cli-problems/5670634#5670634), [three](https://stackoverflow.com/questions/26585714/error-mutex-is-not-supported-when-compiling-with-clr-or-clrpure), [four](https://stackoverflow.com/questions/28447236/workaround-for-mutex-in-native-lib-for-cli-dll) – Hans Passant Apr 11 '21 at 09:31
  • By the way @HansPassant you missed [this](https://stackoverflow.com/questions/31359179/turn-off-clr-option-for-header-file-with-stdmutex?rq=1) one! Also from 5 years ago :-D – Pedro Ferreira Apr 12 '21 at 21:53
  • @WhozCraig so doing this, compiling the unmanaged C++ all together with /clr, am I missing the good old C++ compiler optimizations? Or is it still faster code? – Pedro Ferreira Apr 13 '21 at 14:26

1 Answers1

1

A mixed-mode project can include both unmanaged C++ and managed C++/CLI code. Since <mutex> "is not supported when compiling with /clr" the code that requires it needs to be moved into a separate .cpp file set to compile without /clr. That can be done by adding a new .cpp file to the project, then changing the Property Pages / Configuration Properties / C/C++ / General / Common Language RunTime Support setting from /clr to none for that particular .cpp file (not for the entire project).

  • The code must be moved to a separate file set to compile without /clr in its entirety. Just putting the code inside a #pragma unmanaged block in a file compiled with /clr will not work.

  • If the project uses precompiled headers, the new file must be set to not use the precompiled header, since that one should not be shared between objects built with vs. without /clr.

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • I saw some 10 year old posts with this info, I was hoping for some modern solution but apparently there is no other solution. – Pedro Ferreira Apr 12 '21 at 21:51
  • 1
    @PedroFerreira Granted, C++/CLI is a bit rough around the edges, and I guess it will always be. But the alternatives for interop are a lot worse IMHO, even when counting these /clr idiosyncrasies. – dxiv Apr 12 '21 at 23:03