如何实现一个非管理线程安全收集当我得到这个错误:< mutex>在使用/ clr进行编译时不受支持 [英] How to implement a unmanaged thread-safe collection when I get this error: <mutex> is not supported when compiling with /clr

查看:5641
本文介绍了如何实现一个非管理线程安全收集当我得到这个错误:< mutex>在使用/ clr进行编译时不受支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++应用程序,它由非托管C ++,托管C ++和C#组成。在非托管部分,我试图创建一个线程安全的收集使用std :: mutex。

I have a C++ application which consists of unmanaged C++, managed C++ and c#. In the unmanaged part I'm trying to create a thread safe collection using std::mutex.

但是当我使用mutex时,我得到以下错误:

However when I use the mutex I get the following error;


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

使用/ clr或/ clr:pure进行编译时,不支援C1189:#error:< mutex不能使用互斥?
有人可以推荐替换它,以便我可以创建一个线程安全的非托管集合吗?

Any idea why I can't use the mutex? Can someone recommend a replacement for it so that I can create a thread-safe unmanaged collection?

推荐答案

不支持,因为std :: mutex实现使用GetCurrentThreadId()。这是一个不是应该在托管代码中使用的winapi函数,因为它可能在不使用线程来实现线程的自定义CLR主机上运行。

It is not supported because the std::mutex implementation uses GetCurrentThreadId(). That's a winapi function that is not supposed to be use in managed code since it might be running on a custom CLR host that doesn't use threads to implement threading.

这是一个好的问题,它表明你正在建设你的代码错误。你的本地C ++正在编译/ clr有效。它工作得太好了,所有C ++ 03兼容的代码可以编译为MSIL,并在运行时获得即时编译,就像托管代码。你不希望这种情况发生,你的本地C ++代码应该被编译为机器代码,并获得编译时代码优化器的爱。

This is the good kind of problem to have, it shows that you are building your code wrong. Your native C++ is being compiled with /clr in effect. Which works rather too well, all C++03 compliant code can be compiled to MSIL and get just-in-time compiled at runtime, just like managed code. You don't want this to happen, your native C++ code should be compiled to machine code and get the compile-time code optimizer love.

关闭/ clr选项对于此源代码文件,以及可能的其他项目。右键单击+属性,常规。如果 mutex 出现在你必须在C ++ / CLI源文件中包含的.h文件中,那么你有一个更大的问题,使用接口或pimpl隐藏实现详情。

Turn off the /clr option for this source code file, and possibly others, in your project. Right-click + Properties, General. If the mutex appears in the .h file that you have to #include in a C++/CLI source file then you have a bigger problem, use an interface or pimpl to hide implementation details.

这篇关于如何实现一个非管理线程安全收集当我得到这个错误:&lt; mutex&gt;在使用/ clr进行编译时不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆