visual-studio-2012c++-clireaderwriterlockslim

ReaderWriterLockSlim missing from System::Threading namespace with C++/CLI


I recently installed Visual Studio 2012 and tried recompiling an existing project which is a mixed mode C++ application with a little C++/cli used to access a .net assembly required by the app.

Part of the C++/CLI implementation uses a ReaderWriterLockSlim class to protect access to a dictionary from multiple threads.

This all compiles and works fine with VS2010. After upgrading to VS0212 and .Net 4.5 however, the project now fails to compile since it cannot find the ReaderWriterLockSlim in the System::Threading namespace.

I'm fairly sure the installation is fine since I can create a new C# project and use ReaderWriterLockSlim without any problems.

A new C++ project shown below also fails. I cant find any reference to this class being deliberately removed for C++/CLI users either on here or google. Has anyone else had a similiar experience.

#include "stdafx.h"
using namespace System::Threading;

int _tmain(int argc, _TCHAR* argv[])
{
    ReaderWriterLockSlim^ rwlock = gcnew ReaderWriterLockSlim();

    return 0;
}

Solution

  • Be sure to pay attention to the MSDN Library article for the class when you have a problem like this. Right at the top of the article it shows you which reference assembly is required to use the class:

    Assembly: System.Core (in System.Core.dll)

    Which is not included by default in the C++/CLI project template. Fix that with Project + Properties, Common Properties, Framework and References. Review the "References" list. Click the Add New Reference button and tick System.Core from the Assemblies + Framework list.