【问题标题】:boost::mutex supports try_lock_for in Visual Studio, but not in Xcodeboost::mutex 在 Visual Studio 中支持 try_lock_for,但在 Xcode 中不支持
【发布时间】: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


【解决方案1】:

好的,解决了(容易修复)。我用 boost::timed_mutex 替换了 boost::mutex,现在 VS2008 和 Xcode 都在编译,代码没有错误。

【讨论】:

  • 是的,如果你需要TimedLockable 的概念,你必须使用它的一种模型(比如timed_mutex)。事实证明,mutex 的 Windows 版本也实现了这个概念,但这只是一个实现细节——不应该依赖它。
猜你喜欢
  • 2021-11-30
  • 2022-12-19
  • 1970-01-01
  • 2023-03-08
  • 2020-08-17
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多