问题描述
我正在 Visual Studio 2008 C# 中开发 64 位,我想使用一个使用 boost 的库.所以我用 C++/CLI 写了一个包装器.我设法针对我遇到的错误
I'm developing in Visual Studio 2008 C# for 64bit and I want to use to use a library which uses boost. So I wrote a wrapper in C++/CLI. I managed to target the error I get to
#include <boost/thread/mutex.hpp>.
如果我在 C++/CLI 包装器中包含任何本身包含 的文件,或者如果我直接将它包含在包装器中,我会得到一个System.AccessViolationException" "试图读取或写入受保护的内存.这通常表明其他内存已损坏."
If I include any file in my C++/CLI wrapper that by itself includes <boost/thread/mutex.hpp>
or if I include it directly in the wrapper I get a "System.AccessViolationException" "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
我在为 64 位构建所有东西时非常小心,所以我怀疑问题是否存在.当我在普通"C++ 中使用 64 位相同的库时,一切正常.我看了几篇帖子,其中人们似乎对 boost 线程有类似的问题,但我发现的解决方案都没有奏效.有人有想法吗?
I was very carefull in building everything for 64bit so I doubt that the problems is there. When I use the same library in 64 bit in "plain" C++ everything works fine. I came over a couple of posts where people seem to have similar problems with boost threads but none of the solutions I found worked. Does anyone have an idea?
推荐答案
问题在于 boost.thread 使用了一些 #pragma section
指令,这些指令在没有/clr 的情况下构建时不兼容,然后静态链接到代码使用/clr.
The problem is that boost.thread uses some #pragma section
directives that are incompatible when built without /clr then statically linked to code that uses /clr.
我听说用/clr 重建 boost.thread(即,在调用 bjam 时传递 cxxflags="/clr"
)可以解决这个问题,但我没有亲自尝试过.
I've heard that rebuilding boost.thread with /clr (i.e., pass cxxflags="/clr"
when invoking bjam) fixes the issue, but I haven't tried it personally.
我假设动态链接到 boost.thread(而不是静态链接,这是 VC++ 的默认设置;#define BOOST_THREAD_DYN_LINK
在包含任何 boost 标头之前)也应该可以工作,但同样,我没有试过了.
I assume that dynamically linking to boost.thread (rather than statically, which is the default for VC++; #define BOOST_THREAD_DYN_LINK
before including any boost headers) should work too, but again, I haven't tried it.
如果这不起作用,请尝试使用谷歌搜索 boost
thread
clr
tls
的某种组合;你应该在 boost 邮件列表上找到很多关于它的帖子,因为这是一个老问题.
If that doesn't work, try googling for some combination of boost
thread
clr
tls
; you should find quite a few posts on the boost mailing list about it, as this is an old problem.
编辑:如评论此处 由 Raman Sharma(Microsoft 的高级 PM)撰写,即使 std::mutex
也不支持/clr,因此它不是真实的令人惊讶的是 boost.thread 的互斥实现也不是.
EDIT: As commented here by Raman Sharma (a senior PM at Microsoft), even std::mutex
isn't supported with /clr, so it's no real surprise that boost.thread's mutex implementation isn't either.
这篇关于提升互斥量 C++/CLI 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!