Я не мог понять, как перевести ваш код в тупик.Мне удалось получить следующий пример для взаимоблокировки в macOS:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
typedef struct {
const char *mutex_name;
pthread_mutex_t *pmutex;
} mutex_data_t;
typedef struct {
const char *thread_name;
mutex_data_t mutex_list[2];
} thread_data_t;
void *deadlock(void *data) {
thread_data_t *pthread_data = data;
const char *name = pthread_data->thread_name;
printf("%s: Starting...\n", name);
printf("%s: Locking %s...\n", name, pthread_data->mutex_list[0].mutex_name);
pthread_mutex_lock(pthread_data->mutex_list[0].pmutex);
printf("%s: Sleeping...\n", name);
sleep(1);
printf("%s: Locking %s...\n", name, pthread_data->mutex_list[1].mutex_name);
pthread_mutex_lock(pthread_data->mutex_list[1].pmutex);
printf("Done\n");
return NULL;
}
int main()
{
pthread_mutex_t mutex1;
pthread_mutex_t mutex2;
mutex_data_t mutex1_data = { "mutex1", &mutex1 };
mutex_data_t mutex2_data = { "mutex2", &mutex2 };
thread_data_t thread1_data = { "thread1", { mutex1_data, mutex2_data }};
thread_data_t thread2_data = { "thread2", { mutex2_data, mutex1_data }};
pthread_t thread1;
pthread_t thread2;
pthread_mutex_init(&mutex1, NULL);
pthread_mutex_init(&mutex2, NULL);
pthread_create(&thread1, NULL, deadlock, &thread1_data);
pthread_create(&thread2, NULL, deadlock, &thread2_data);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Вывод
thread1: Starting...
thread2: Starting...
thread1: Locking mutex1...
thread2: Locking mutex2...
thread1: Sleeping...
thread2: Sleeping...
thread1: Locking mutex2...
thread2: Locking mutex1...