【发布时间】:2013-10-15 14:09:09
【问题描述】:
我使用的是 Xcode 5.0 和 boost 1.54。以下代码使用 Visual Studio 2008 Sp1 可以正常编译,但无法在 Xcode 中编译:
template <class Rep, class Period>
bool try_lock_for(const boost::chrono::duration<Rep, Period> &_rel_time)
{
return m_mutex.try_lock_for(_rel_time);
}
我在 Xcode 中收到错误“在 'boost::mutex' 中没有名为 'try_lock_until' 的成员”。
我包括 boost/mutex.hpp 和 boost/thread.hpp。 Xcode 项目设置为使用 Apple LLVM 5.0 和 GNU99 C++ 方言。当我使用 'try_lock_until(...)' 时出现类似的编译错误。
似乎 boost::mutex 在两个平台上的定义不同。在 Windows 上,代码如下所示:
namespace detail
{
typedef ::boost::detail::basic_timed_mutex underlying_mutex;
}
class mutex:
public ::boost::detail::underlying_mutex
{
...
}
所以在 Windows 上 boost::mutex 继承自定时互斥锁,但在 xcode 上它只是一个使用 pthreads 的类(不支持 try_for 等)。
那么在 Windows 和 MacOS 上使用 boost mutex(带有 try_for)的正确方法是什么?任何帮助将不胜感激。
【问题讨论】:
-
但是 boost::mutex 的接口中确实没有这些功能。也许您使用了一些
typedef,它在一个平台上解析为boost::mutex,但在另一个平台上解析为boost::timed_mutex(或左右)?你能准备一份sscce吗? -
是的,我检查过,这是正确的。在 Windows 和 MacOS 上,boost mutex 的定义不同(我将相应地编辑问题,因为我仍然无法使用 Xcode 编译)。
标签: c++ xcode visual-studio boost boost-thread