一、 互斥量std::mutex
C++中通过实例化std::mutex创建互斥量实例,通过成员函数lock()对互斥量上锁,unlock()进行解锁。C++中与std::mutex相关的类(包括锁类型)和函数都声明在< mutex >头文件中,要使用std::mutex,就必须包含此头文件。
#include <mutex> #include <thread> #include <iostream> int counter = 0; std::mutex mtx; // 创建互斥量 void add() { for (int i = 0; i < 1000000; i++) { mtx.lock(); // 加锁 counter = counter + 1; mtx.unlock(); // 解锁 } } void sub() { for (int i = 0; i < 1000000; i++) { mtx.lock(); // 加锁 counter = counter - 1; mtx.unlock(); // 解锁 } } int main() { std::thread t1(add); std::thread t2(sub); t1.join(); t2.join(); std::cout << "counter:\t" << counter << std::endl; }
二、 模板类std::lock_guard
不过,实践中不推荐直接去调用成员函数,调用成员函数就意味着,必须在每个函数出口都要去调用unlock(),也包括异常的情况。C++标准库为互斥量提供了一个RAII语法的模板类std::lock_guard,在构造时就能提供已锁的互斥量,并在析构的时候进行解锁,从而保证了一个已锁互斥量能被正确解锁。下面的程序清单中,展示了如何在多线程应用中,使用std::mutex构造的std::lock_guard实例,对一个共享数据访问保护。std::mutex和std::lock_guard都在 < mutex >头文件中声明。
#include <mutex> #include <thread> #include <iostream> int counter = 0; std::mutex mtx; // 创建互斥量 void add() { for (int i = 0; i < 1000000; i++) { std::lock_guard<std::mutex> lock(mtx); // 析构,自动解锁。 counter = counter + 1; } } void sub() { for (int i = 0; i < 1000000; i++) { std::lock_guard<std::mutex> lock(mtx); // 析构,自动解锁。 counter = counter - 1; } } int main() { std::thread t1(add); std::thread t2(sub); t1.join(); t2.join(); std::cout << "counter:\t" << counter << std::endl; }