boost::mutex 在 Visual Studio 中支持 try_lock_for,但在 Xcode 中不支持

boost::mutex supports try_lock_for in Visual Studio, but not in Xcode

本文关键字:lock for 但在 不支持 Xcode 支持 mutex Visual Studio boost try      更新时间:2023-10-16

我正在使用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互斥锁(带try_for)的正确方法是什么?任何帮助将不胜感激。

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