C++ 线程安全注解

实例开篇

线程安全注解是现代C++开发的机制,通常在编译期可以帮助发现一些线程安全问题。

下面直接从实例中来理解。

cpp 复制代码
class Account {
private:
    Mutex mu;
    int money GUARDED_BY(mu);

    void Sub(int amount) {
        money -= amount;    // writing variable 'money' requires holding mutex 'mu' exclusively
    }

    void Add(int amount) REQUIRES(mu) {
        money += amount;
    }
    
public:
    void Withdraw(int amount) {
        mu.Lock();
        Sub(amount);    // warning: mutex 'mu' is still held at the end of function
    }

    void Deposit(int amount) {
        mu.Lock();
        Add(amount);
        mu.Unlock();
    }

    void TransferFromOthers(Account& b, int amount) {
        mu.Lock();
        
        Sub(amount);
        
        b.Add(amount);  // calling function 'Add' requires holding mutex 'b.mu' exclusively
        
        mu.Unlock();
    }
};

这段是一个账户的例子。

接口

向外暴露的接口为:

  • void Withdraw(int amount) 取款

    取款调用了 void Sub(int amount)

    而这里在 Withdraw 函数中也确实在调用 Sub 函数前已经持有锁 mu 了,但是这里并未对 Sub 函数添加 REQUIRES(mu) 注解,所以 Sub 函数本身不能保证在此已经持有锁 mu ,所以触发 Warning mutex 'mu' is still held at the end of function

    但是在 Sub 函数结束后,意味着需要释放锁,否则其他操作将无法获得锁 mu ,造成死锁问题。

  • void Deposit(int amount) 存款

    存款调用了 void Add(int amount)

    在进入 Add 函数前持有了锁 mu ,在从 Add 函数返回后也释放了锁 mu ,所以无报错。

    对于 Add 函数,其中添加了 REQUIRES(mu) 注解,如果存在某个调用 Add 的函数未在进入 Add 函数前持有锁 mu,则会在调用 Add 函数的部分触发 Warning calling function 'Add' requires holding mutex 'mu' exclusively 。如下代码所示

    cpp 复制代码
    void Deposit(int amount) {
        Add(amount); // calling function 'Add' requires holding mutex 'mu' exclusively
    }
  • void TransferToOthers(Account& b, int amount) 当前账号向账户 b 转账

    这个操作应该是个原子操作,这里先是减少当前账户余额,然后增加账户 b 的余额。

    Sub 需要加锁,加的是当前账户的锁 mu

    b.Add 也需要加锁,加的是账户 b 的锁 b.mu

    但是这里在进入 b.Add 函数前,当前函数并未持有锁 b.mu ,所以触发 Warning calling function 'Add' requires holding mutex 'b.mu' exclusively

如何修改呢,按照上述所述修改即可

cpp 复制代码
private:
    Mutex mu;
    int money GUARDED_BY(mu);

    void Sub(int amount) REQUIRES(mu) {
        money -= amount;
    }

    void Add(int amount) REQUIRES(mu) {
        money += amount;
    }

public:
    void Withdraw(int amount) {
        mu.Lock();
        Sub(amount);
        mu.Unlock();
    }

    void Deposit(int amount) {
        mu.Lock();
        Add(amount);
        mu.Unlock();
    }

    void TransferToOthers(Account& b, int amount) {
        mu.Lock();

        Sub(amount);

        b.mu.Lock();
        b.Add(amount);
        b.mu.Unlock();
        
        mu.Unlock();
    }
};

线程安全注解

这其中涉及到两个注解:

  • GUARDED_BY(mu) ,用于标识共享变量的保护互斥量。表示使用该变量时必须获得锁 mu
  • REQUIRES(mu) ,用于标识函数在调用时需要独占地持有指定的互斥量。表示进入该函数前必须获得锁 mu

在多线程编程中,当多个线程同时访问共享资源时,需要确保某些操作的执行是互斥的,即同一时间只能由一个线程执行。互斥操作通常涉及对共享资源的修改或对多个资源的原子性操作。

Mutex 定义

cpp 复制代码
class LOCKABLE Mutex {
public:
    Mutex() = default;
    ~Mutex() = default;

    Mutex(const Mutex&) = delete;
    Mutex& operator=(const Mutex&) = delete;

    void Lock() EXCLUSIVE_LOCK_FUNCTION() { mu_.lock(); }
    void Unlock() UNLOCK_FUNCTION() { mu_.unlock(); }
    void AssertHeld() ASSERT_EXCLUSIVE_LOCK() {}

private:
    std::mutex mu_;
};

这里涉及到了四个注解:

  • LOCKABLE 一个自定义的宏定义或类型定义,表示这是一个可被加锁的类

  • EXCLUSIVE_LOCK_FUNCTION() 表示被标记的函数是一个独占锁函数,调用这个函数会获取一个独占锁。

  • UNLOCK_FUNCTION() 表示被标记的函数是一个解锁函数,调用这个函数会释放之前获取的独占锁。

  • ASSERT_EXCLUSIVE_LOCK() 表示被标记的函数用于断言当前线程持有一个独占锁,确保调用该函数的线程在调用时持有独占锁。

更多的线程安全注解,求STAR!!!

参考

相关推荐
后端小张14 小时前
【JAVA进阶】Spring Boot 核心知识点之自动配置:原理与实战
java·开发语言·spring boot·后端·spring·spring cloud·自动配置
tg-zm88999619 小时前
2025返利商城源码/挂机自动收益可二开多语言/自定义返利比例/三级分销理财商城
java·mysql·php·laravel·1024程序员节
X***C86219 小时前
SpringBoot:几种常用的接口日期格式化方法
java·spring boot·后端
阿巴~阿巴~19 小时前
JsonCpp:C++ JSON处理利器
linux·网络·c++·json·tcp·序列化和反序列化
j***294820 小时前
IPV6公网暴露下的OPENWRT防火墙安全设置(只允许访问局域网中指定服务器指定端口其余拒绝)
服务器·安全·php
前端达人20 小时前
你的App消息推送为什么石沉大海?看Service Worker源码我终于懂了
java·开发语言
小光学长20 小时前
基于ssm的宠物交易系统的设计与实现850mb48h(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·前端·数据库
编程大师哥20 小时前
vxe-table 透视表分组汇总及排序基础配置
java
8***848220 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
9***J62820 小时前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端