【发布时间】:2016-11-08 17:35:48
【问题描述】:
我在 clang 线程安全模型之后实现了以下互斥类(希望如此)。 (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_; } );
}
}
};
我收到铿锵警告:
warning: reading variable 'count_' requires holding mutex 'mutex_' [-Wthread-safety-analysis].
编译器的警告(带有-Wthread-safety的clang 3.8)是否正确?如果是,违规行为究竟是如何发生的?
【问题讨论】:
标签: c++ thread-safety clang++