Clang线程安全分析与c++标准库
Clang thread safety analysis with C++ standard library
这描述了如何使用c++中的注释完成静态线程安全分析:http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
我如何使用标准类型,如std::mutex和std::lock_guard?
mutex.h的示例代码注释了一个自定义接口。我有类型"互斥",这是定义在那里,并实现一个类使用std::互斥与注释方法或Clang带来注释类型的某种方式?
在clang的最新版本中,您可能不再需要包装std::mutex了,因为自2016年3月15日以来,线程安全注释已包含在其中。
为std::mutex和添加了clang线程安全注释Std::lock_guard使使用这些类型的代码可以直接使用这些类型而不是必须包装类型来提供注释。这些检查当启用- thread-safety时,提供简单但有用的静态检查是否存在潜在的竞争条件
详情见http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
所以只要有-Wthread-safety
就足够了。
实现所提供的mutex.h文件中描述的接口,并使用std::mutex类来实现。也就是说,这是一个半成品的实现:
对mutex.h文件进行了小修改,以包含std::mutex对象
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.
然后在mutex.cpp
中实现其余部分#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();
}
相关文章:
- 使用CMake检测支持的C++标准
- 如何理解C++标准N3337中的expr.const.cast子句8
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 编译标准库类型
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 铸造标准::有没有回到原来的类型
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- this_thread::sleep_for和计时时钟之间的关系是否由C++11标准指定
- 标准库类型的赋值运算符的引用限定符
- 标准是否严格定义了该程序应该如何编译?
- 如何从Windows应用程序输出到标准?
- 安全到标准:移动会员?
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 这是否符合C++标准:双响双响,例如!!(-0.0).
- 标准::变体的赋值运算符
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 如何在 Mac 上使用 c++17 并行标准库算法?
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 并行标准::复制复杂性