[c++11]多线程编程(四)——死锁(Dead Lock)

博客主要介绍了 C++ 中的死锁问题,当一个线程上锁后不释放,另一个线程访问该锁保护的资源时就会发生死锁。还给出了避免死锁的解决办法,如比较锁地址、使用层次锁等,同时总结了避免死锁的几点建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

死锁

如果你将某个mutex上锁了,却一直不释放,另一个线程访问该锁保护的资源的时候,就会发生死锁,这种情况下使用lock_guard可以保证析构的时候能够释放锁,然而,当一个操作需要使用两个互斥元的时候,仅仅使用lock_guard并不能保证不会发生死锁,如下面的例子:

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <fstream>
using namespace std;

class LogFile {
    std::mutex _mu;
    std::mutex _mu2;
    ofstream f;
public:
    LogFile() {
        f.open("log.txt");
    }
    ~LogFile() {
        f.close();
    }
    void shared_print(string msg, int id) {
        std::lock_guard<std::mutex> guard(_mu);
        std::lock_guard<std::mutex> guard2(_mu2);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
    void shared_print2(string msg, int id) {
        std::lock_guard<std::mutex> guard(_mu2);
        std::lock_guard<std::mutex> guard2(_mu);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
};

void function_1(LogFile& log) {
    for(int i=0; i>-100; i--)
        log.shared_print2(string("From t1: "), i);
}

int main()
{
    LogFile log;
    std::thread t1(function_1, std::ref(log));

    for(int i=0; i<100; i++)
        log.shared_print(string("From main: "), i);

    t1.join();
    return 0;
}

运行之后,你会发现程序会卡住,这就是发生死锁了。程序运行可能会发生类似下面的情况:

Thread A              Thread B
_mu.lock()             _mu2.lock()
   //死锁               //死锁
_mu2.lock()         _mu.lock()

解决办法有很多:

  1. 可以比较mutex的地址,每次都先锁地址小的,如:

    if(&_mu < &_mu2){
        _mu.lock();
        _mu2.unlock();
    }
    else {
        _mu2.lock();
        _mu.lock();
    }
  2. 使用层次锁,将互斥锁包装一下,给锁定义一个层次的属性,每次按层次由高到低的顺序上锁。

这两种办法其实都是严格规定上锁顺序,只不过实现方式不同。

c++标准库中提供了std::lock()函数,能够保证将多个互斥锁同时上锁,

std::lock(_mu, _mu2);

同时,lock_guard也需要做修改,因为互斥锁已经被上锁了,那么lock_guard构造的时候不应该上锁,只是需要在析构的时候释放锁就行了,使用std::adopt_lock表示无需上锁:

std::lock_guard<std::mutex> guard(_mu2, std::adopt_lock);
std::lock_guard<std::mutex> guard2(_mu, std::adopt_lock);

完整代码如下:

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <fstream>
using namespace std;

class LogFile {
    std::mutex _mu;
    std::mutex _mu2;
    ofstream f;
public:
    LogFile() {
        f.open("log.txt");
    }
    ~LogFile() {
        f.close();
    }
    void shared_print(string msg, int id) {
        std::lock(_mu, _mu2);
        std::lock_guard<std::mutex> guard(_mu, std::adopt_lock);
        std::lock_guard<std::mutex> guard2(_mu2, std::adopt_lock);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
    void shared_print2(string msg, int id) {
        std::lock(_mu, _mu2);
        std::lock_guard<std::mutex> guard(_mu2, std::adopt_lock);
        std::lock_guard<std::mutex> guard2(_mu, std::adopt_lock);
        f << msg << id << endl;
        cout << msg << id << endl;
    }
};

void function_1(LogFile& log) {
    for(int i=0; i>-100; i--)
        log.shared_print2(string("From t1: "), i);
}

int main()
{
    LogFile log;
    std::thread t1(function_1, std::ref(log));

    for(int i=0; i<100; i++)
        log.shared_print(string("From main: "), i);

    t1.join();
    return 0;
}

总结一下,对于避免死锁,有以下几点建议:

  1. 建议尽量同时只对一个互斥锁上锁。

    {
        std::lock_guard<std::mutex> guard(_mu2);
        //do something
        f << msg << id << endl;
    }
    {
        std::lock_guard<std::mutex> guard2(_mu);
        cout << msg << id << endl;
    }
  2. 不要在互斥锁保护的区域使用用户自定义的代码,因为用户的代码可能操作了其他的互斥锁。

    {
        std::lock_guard<std::mutex> guard(_mu2);
        user_function(); // never do this!!!
        f << msg << id << endl;
    }
  3. 如果想同时对多个互斥锁上锁,要使用std::lock()
  4. 给锁定义顺序(使用层次锁,或者比较地址等),每次以同样的顺序进行上锁。详细介绍可看C++并发编程实战

参考

  1. C++并发编程实战
  2. C++ Threading #4: Deadlock
C++ 多线程编程中,常见的并发问题包括数据竞争、死锁、资源饥饿以及线程同步等。为了解决这些问题,C++11 引入了标准库中的多线程支持,并提供了多种机制来确保线程安全和高效执行。 ### 数据竞争的解决方案 数据竞争发生在多个线程同时访问共享数据而没有适当的同步机制时。为了避免这种情况,可以使用互斥量(`std::mutex`)来保护共享资源[^1]。例如: ```cpp #include <iostream> #include <thread> #include <mutex> std::mutex mtx; // Mutex for critical section int shared_data = 0; void increment() { for (int i = 0; i < 100000; ++i) { mtx.lock(); ++shared_data; mtx.unlock(); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Shared data: " << shared_data << std::endl; return 0; } ``` 在这个例子中,`std::mutex` 被用来锁定对 `shared_data` 的访问,确保每次只有一个线程能够修改它。 ### 死锁的预防 死锁通常发生在两个或更多线程互相等待对方释放资源时。一种避免死锁的方法是使用 `std::lock` 和 `std::unique_lock` 来尝试获取多个锁而不导致死锁[^4]。此外,还可以采用固定顺序加锁策略,即所有线程都按照相同的顺序请求锁。 ```cpp #include <iostream> #include <thread> #include <mutex> #include <vector> std::mutex mtx1, mtx2; void dead_lock_avoidance() { // Lock both mutexes without deadlock std::lock(mtx1, mtx2); // Adopt locks std::unique_lock<std::mutex> lock1(mtx1, std::adopt_lock); std::unique_lock<std::mutex> lock2(mtx2, std::adopt_lock); // Critical section std::cout << "Thread id: " << std::this_thread::get_id() << " has acquired both locks." << std::endl; } int main() { std::thread t1(dead_lock_avoidance); std::thread t2(dead_lock_avoidance); t1.join(); t2.join(); return 0; } ``` ### 线程同步与通信 为了协调不同线程之间的操作,可以使用条件变量(`std::condition_variable`)来进行线程间的通信。这允许一个或多个线程等待某个特定条件的发生。 ```cpp #include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::condition_variable cv; std::mutex cv_m; bool ready = false; void print_id(int id) { std::unique_lock<std::mutex> lck(cv_m); while (!ready) { cv.wait(lck); } std::cout << "thread " << id << '\n'; } void go() { std::unique_lock<std::mutex> lck(cv_m); ready = true; cv.notify_all(); } int main() { std::thread threads[10]; // spawn 10 threads: for (int i = 0; i < 10; ++i) threads[i] = std::thread(print_id, i); std::cout << "10 threads ready to race...\n"; go(); // go! for (auto& th : threads) th.join(); return 0; } ``` 在这个示例中,`std::condition_variable` 用于通知所有等待的线程当 `ready` 变量变为 `true` 时开始执行。 通过这些方法和技术,可以在 C++ 中有效地处理多线程编程中的并发问题,从而编写出既安全又高效的并发程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值