Can I use std::thread from a C++/CLI project in Visual Studio 2012

This question seems to produce some confusing and contradicting answers, however the short answer to it is ‘yes, you can’.

You can use std::thread in unmanaged code in a C++/CLI project but not from managed code – but the good thing about C++/CLI is that you can mix managed and unamanaged code.

if you try to use it from managed code you will get an errors like this:

error directive: is not supported when compiling with /clr or /clr:pure.

and this:

error directive: ERROR: Concurrency Runtime is not supported when compiling /clr.

To mark a C++ file as un-managed, go to its C/C++ settings and set its ‘Common Language RunTime Support’ setting to ‘No Common Language RunTime Support’. You can also use the ‘managed’ and ‘unmanaged’ pragmas in your code to do the same, but something about mixing the two in a single file creeps me out!

Possibly the best thing to do is to keep managed code to a minimum (as it’s as ugly as sin) and just use it to interface your .NET code to your native C++ code – which can use std::thread et al. to its heart’s content!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *