Deadlocks that occur frequently

 Single thread deadlock


#include <mutex>

std::mutex mutex1;    
    
/////////// Lock is called again in the locked state    
void deadlock_1() {    
    std::lock_guard<std::mutex> lockGuard(mutex1);    
    deadlock_1_1();    
}

     
void deadlock_1_1() {    
    std::lock_guard<std::mutex> lockGuard(mutex1); // Attempt to lock again on a locked lock   
}    
     
////////// When the lock is applied, it spins around and calls the lock again, if it spins a lot and the lock is caught...    
void deadlock_2() {    
    std::lock_guard<std::mutex> lockGuard(mutex1);    
    deadlock_2_1();    
}    

void deadlock_2_1() {
    deadlock_2();                               // Try lock again with recursive call
    
}

Multi Thread Deadlock - Deadlock occurs when executed in numerical order

#include <mutex>


std::mutex mutex1;    
std::mutex mutex2; 

void deadlock_3_thread_1() {
    std::lock_guard<std::mutex> lockGuard(mutex1);    // 1   
    std::lock_guard<std::mutex> lockGuard(mutex2);    // 3
}                         
                          
void deadlock_3_thread_2() {
    std::lock_guard<std::mutex> lockGuard(mutex2);    // 2
    std::lock_guard<std::mutex> lockGuard(mutex1);    // 3
}

No comments:

Lognote - My toy project

In a project, the code work is limited When I say, "I think it will work if I change it like this," I get, "If it doesn't...