c++ std::mutex和std::lock_guard怎么用_c++互斥锁机制与线程同步实例

std::mutex和std::lock_guard用于C++线程同步,前者提供加锁解锁功能,后者通过RAII机制自动管理锁,防止数据竞争。

在C++多线程编程中,std::mutexstd::lock_guard 是实现线程同步的基本工具。它们用于保护共享数据,防止多个线程同时访问导致数据竞争和未定义行为。

std::mutex:互斥锁的基本使用

std::mutex 是一个同步原语,用来确保同一时间只有一个线程可以进入临界区(即访问共享资源的代码段)。

常用成员函数:

  • lock():获取锁,如果已被其他线程持有,则阻塞当前线程。
  • unlock():释放锁,必须由持有锁的线程调用。
  • try_lock():尝试获取锁,不阻塞;成功返回 true,否则返回 false。

直接使用 lock/unlock 容易出错,比如忘记 unlock 或异常导致提前退出。因此推荐配合 RAII 机制使用。

std::lock_guard:自动管理锁的生命周期

std::lock_guard 是一个RAII风格的锁管理类。它在构造时自动加锁,析构时自动解锁,确保即使发生异常也能正确释放锁。

使用步骤:

  • 定义一个 std::mutex 成员或变量。
  • 在需要保护的代码块中创建 std::lock_guard 对象,传入 mutex。
  • 作用域结束时,lock_guard 自动析构并释放锁。

线程同步实例:银行账户存取款模拟

下面是一个使用 std::mutex 和 std::lock_guard 保护共享账户余额的完整例子:

#include 
#include 
#include 
#include 

class BankAccount {
private:
    double balance;
    std::mutex mtx;  // 互斥锁

public:
    BankAccount(double initial) : balance(initial) {}

    void deposit(double amount) {
        std::lock_guard lock(mtx);  // 自动加锁
        balance += amount;
        std::cout << "Deposited " << amount 
                  << ", new balance: " << balance << std::endl;
    }

    void withdraw(double amount) {
        std::lock_guard lock(mtx);
        if (balance >= amount) {
            balance -= amount;
            std::cout << "Withdrew " << amount 
                      << ", new balance: " << balance << std::endl;
        } else {
            std::cout << "Failed to withdraw " << amount 
                      << ", insufficient funds" << std::endl;
        }
    }

    double get_balance() const {
        std::lock_guard lock(mtx);
        return balance;
    }
};

void worker(BankAccount& account) {
    for (int i = 0; i < 5; ++i) {
        account.deposit(100);
        account.withdraw(50);
    }
}

int main() {
    BankAccount account(1000);
    std::vector threads;

    // 创建 3 个线程同时操作账户
    for (int i = 0; i < 3; ++i) {
        threads.emplace_back(worker, std::ref(account));
    }

    for (auto& t : threads) {
        t.join();
    }

    std::cout << "Final balance: " << account.get_balance() << std::endl;
    return 0;
}

输出示例(顺序可能不同):

Deposited 100, new balance: 1100
Withdrew 50, new balance: 1050
Deposited 100, new balance: 1150
...
Final balance: 1750

由于每个操作都被 std::lock_guard 保护,不会出现余额计算错误或打印混乱的情况。

注意事项与最佳实践

使用互斥锁时要注意以下几点:

  • 避免死锁:多个 mutex 时要始终按相同顺序加锁。
  • 锁的粒度不宜过大,否则影响并发性能。
  • 不要在持有锁时执行耗时操作(如 I/O、网络请求)。
  • 尽量使用 std::lock_guard 或 std::scoped_lock,避免手动调用 lock/unlock。
  • const 成员函数若需加锁,应将 mutex 定义为 mutable。

基本上就这些。std::mutex 配合 std::lock_guard 提供了简单可靠的线程安全机制,是C++多线程编程的基石之一。