37 lines
1.3 KiB
C
37 lines
1.3 KiB
C
#include <stdio.h>
|
|
#include <pthread.h>
|
|
#include "race.h"
|
|
|
|
int main() {
|
|
pthread_t participants[NUM_PARTICIPANTS];
|
|
int participant_ids[NUM_PARTICIPANTS];
|
|
|
|
// Инициализация мьютекса и условных переменных
|
|
pthread_mutex_init(info->tunnel_mutex, NULL);
|
|
pthread_cond_init(info->start_cond, NULL);
|
|
pthread_cond_init(info->finish_cond, NULL);
|
|
|
|
// Создание участников гонки
|
|
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
|
|
participant_ids[i] = i + 1;
|
|
pthread_create(&participants[i], NULL, participant, &participant_ids[i]);
|
|
}
|
|
|
|
// Ожидание завершения гонки
|
|
pthread_mutex_lock(info->tunnel_mutex);
|
|
while (info->participants_finished < NUM_PARTICIPANTS) {
|
|
// Ждем, пока все участники не закончат гонку
|
|
pthread_cond_wait(info->finish_cond, info->tunnel_mutex);
|
|
}
|
|
pthread_mutex_unlock(info->tunnel_mutex);
|
|
|
|
printf("Гонка завершена! Победитель - участник %d\n", participant_ids[NUM_PARTICIPANTS - 1]);
|
|
|
|
// Очистка ресурсов
|
|
pthread_mutex_destroy(info->tunnel_mutex);
|
|
pthread_cond_destroy(info->start_cond);
|
|
pthread_cond_destroy(info->finish_cond);
|
|
|
|
return 0;
|
|
}
|