39 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
//
 | 
						||
// Created by mrnek on 25.05.2023.
 | 
						||
//
 | 
						||
#include "race.h"
 | 
						||
 | 
						||
void * participant(void *arg) {               // Функция для участника гонки
 | 
						||
    info->participants_in_tunnel = 0;
 | 
						||
    info->participants_finished = 0;
 | 
						||
    int participant_id = *(int *)arg;
 | 
						||
 | 
						||
    // Подготовка участника
 | 
						||
    printf("Участник %d готовится к гонке\n", participant_id);
 | 
						||
 | 
						||
    // Ожидание начала гонки
 | 
						||
    pthread_mutex_lock(info->tunnel_mutex);
 | 
						||
    while (info->participants_in_tunnel >= TUNNEL_CAPACITY) {
 | 
						||
        // Ждем, пока не освободится место в тоннеле
 | 
						||
        pthread_cond_wait(info->start_cond, info->tunnel_mutex);
 | 
						||
    }
 | 
						||
    info->participants_in_tunnel++;
 | 
						||
    pthread_mutex_unlock(info->tunnel_mutex);
 | 
						||
 | 
						||
    printf("Участник %d начинает гонку\n", participant_id);
 | 
						||
 | 
						||
    // Гонка
 | 
						||
 | 
						||
    // Завершение гонки
 | 
						||
    pthread_mutex_lock(info->tunnel_mutex);
 | 
						||
    info->participants_finished++;
 | 
						||
    if (info->participants_finished == NUM_PARTICIPANTS) {
 | 
						||
        // Если все участники закончили гонку, уведомляем о завершении
 | 
						||
        pthread_cond_broadcast(info->finish_cond);
 | 
						||
    }
 | 
						||
    pthread_mutex_unlock(info->tunnel_mutex);
 | 
						||
 | 
						||
    printf("Участник %d закончил гонку\n", participant_id);
 | 
						||
 | 
						||
    pthread_exit(NULL);
 | 
						||
} |