我有以下互斥锁类(希望)在after线程安全模型之后实现。 (http://clang.llvm.org/docs/ThreadSafetyAnalysis.html)

class CAPABILITY( "mutex" ) Mutex : public std::mutex
{
  public:
    void lock() ACQUIRE()
    {
        std::mutex::lock();
    }

    void unlock() RELEASE()
    {
        std::mutex::unlock();
    }
};

class SCOPED_CAPABILITY LockGuard : public std::unique_lock< std::mutex >
{
  public:
        LockGuard( Mutex& mu ) ACQUIRE( mu ) : std::unique_lock< std::mutex >( mu )
        {
        }

        ~LockGuard() RELEASE()
        {
        }
};

用法如下:
class Barrier
{
    ...

    Mutex mutex_;
    std::condition_variable cv_ GUARDED_BY( mutex_ );
    std::size_t min_count_ GUARDED_BY( mutex_ );
    std::size_t count_ GUARDED_BY( mutex_ );

    bool Barrier::waitFor( const std::chrono::microseconds& duration )
    {
        LockGuard lock( mutex_ );
        if ( count_ >= min_count_ )
        {
            return true;
        }
        else
        {
            // WARNING IS IN THE LINE BELOW
            return cv_.wait_for( lock, duration, [this]() { return count_ >= min_count_; } );
        }
    }
};

我收到the警告:warning: reading variable 'count_' requires holding mutex 'mutex_' [-Wthread-safety-analysis]

编译器的警告(带有-Wthread-safety的lang 3.8)是否正确?如果是,该违规行为如何发生?

最佳答案

因为对于编译器来说尚不清楚,在哪里评估了lambda表达式,所以也必须对lambda进行注释。

不会产生任何错误的正确行是

return cv_.wait_for( lock, duration, [this]() REQUIRES( mutex_ ) { return count_ >= min_count_; } );

07-27 15:33