C++ 標準ライブラリを使用した Clang スレッド セーフ分析



これは、C++ でアノテーションを使用して静的スレッド セーフ分析を行う方法について説明しています:http://clang.llvm.org/docs/ThreadSafetyAnalysis.html


std::mutex や std::lock_guard などの標準型でこれを使用するにはどうすればよいですか?


mutex.h のサンプル コードは、カスタム インターフェイスに注釈を付けます。そこに定義されている型「Mutex」を持っていて、注釈付きメソッドで std::mutex を使用してクラスを実装していますか?それとも、Clang は何らかの方法で注釈付き型をもたらしますか?


答え:


2016 年 3 月 15 日以降、スレッド セーフ アノテーションが含まれているため、clang の最近のバージョンでは、おそらく std::mutex をラップする必要がなくなりました。



だから単純に -Wthread-safety


いくつかのコードの回答


class CAPABILITY("mutex") Mutex { private:   std::mutex std_mutex;
public: // Acquire/lock this mutex exclusively. Only one thread can have exclusive // access at any one time. Write operations to guarded data require an // exclusive lock.
#include "mutex.h"  void Mutex::Lock(){   this->std_mutex.lock();
} void Mutex::Unlock(){ this->std_mutex.unlock();
} bool Mutex::TryLock(){ return this->std_mutex.try_lock();
}