死锁代码解读:
#include<bits/stdc++.h>
#include "unistd.h"
using namespace std;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;
static int sequence1 = 0;
static int sequence2 = 0;
int func1() {
pthread_mutex_lock(&mutex1);
sequence1++;
cout << sequence1 << endl;
sleep(1);
pthread_mutex_lock(&mutex2);
sequence2++;
cout << sequence2 << endl;
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex1);
return sequence1;
}
int func2() {
pthread_mutex_lock(&mutex2);
sequence2++;
cout << sequence2 << endl;
sleep(1);
pthread_mutex_lock(&mutex1);
sequence1++;
cout << sequence1 << endl;
pthread_mutex_unlock(&mutex1);
pthread_mutex_unlock(&mutex2);
return sequence2;
}
void *thread1(void *arg) {
while (1) {
int v = func1();
cout << v << endl;
if (v == 100000) {
// exit thread
pthread_exit(NULL);
}
}
}
void *thread2(void *arg) {
while (1) {
int v = func2();
cout << v << endl;
if (v == 100000) {
pthread_exit(NULL);
}
}
}
void *thread3(void *arg) {
while (1) {
sleep(1);
char szBuf[128];
memset(szBuf, 0, sizeof(szBuf));
// copy string
strcpy(szBuf, "thread3");
cout << "thread3 copied" << endl;
}
}
void *thread4(void *arg) {
while (1) {
sleep(1);
char szBuf[128];
memset(szBuf, 0, sizeof(szBuf));
strcpy(szBuf, "thread3");
cout << "thread4 copied" << endl;
}
}
int main() {
// thread array
pthread_t tid[4];
// create thread an execute
if (pthread_create(&tid[0], NULL, &thread1, NULL) != 0) {
_exit(1);
}
if (pthread_create(&tid[1], NULL, &thread2, NULL) != 0) {
_exit(1);
}
if (pthread_create(&tid[2], NULL, &thread3, NULL) != 0) {
_exit(1);
}
if (pthread_create(&tid[3], NULL, &thread4, NULL) != 0) {
_exit(1);
}
sleep(5);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_join(tid[2], NULL);
pthread_join(tid[3], NULL);
return 0;
}