[Linux] Thread synchronization: mutex lock, read-write lock, condition variable, spin lock, barrier

1. Mutex

1.0 Blocking and Deadlock

Blocking: All locked threads will be blocked;
Deadlock: Two possibilities for deadlock
1) If the thread tries to If the same mutex is locked twice, it will fall into a deadlock state;
2) Thread A keeps occupying the first mutex, and then tries to lock the second mutex When the amount is reached, it enters the blocking state;
Thread B has been occupying the second mutex, and then tries to lock the first mutex, entering the blocking state.

Method to avoid deadlock: All threads always lock the first mutex first, and then lock the second mutex

1.1 C example

#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex;int index = 

Guess you like

Origin blog.csdn.net/u010168781/article/details/134364775