class Program
{
static Mutex Barber = new Mutex();
static int[] seats = new int[8] { 100, 100, 100, 100, 100, 100, 100, 100 };
static Random rand = new Random();
static int x = 0;
static void Main(string[] args)
{
Thread[] myThreads = new Thread[seats.Length];
for (int i = 1; i < 30; i++)
{
Console.WriteLine("Пришел " + $"клиент - {i}");
for (int z = 0; z < seats.Length; z++)
{
if (seats[z] != 100)
{
if (z == seats.Length - 1)
{
Console.WriteLine($"клиент - {i}" + " видит, что все места заняты, и уходит");
}
continue;
}
else if (seats[z] == 100)
{
seats[z] = i;
Console.WriteLine($"клиент - {i}" + $" занял кресло {z + 1}");
myThreads[z] = new Thread(Count);
myThreads[z].Name = "Поток " + i.ToString();
myThreads[z].Start();
break;
}
}
int cooldown = rand.Next(1000, 5000);
Thread.Sleep(cooldown);
}
}
public static void Count()
{
Barber.WaitOne();
Fifo(x);
Console.WriteLine("Парикмахер подходит к покупателю " + seats[x] + " и начинает стричь.");
Thread.Sleep(5000);
Console.WriteLine("Парикмахер заканчивает стрижку");
Console.WriteLine($"Кресло {x} освободилось");
seats[x] = 100;
Barber.ReleaseMutex();
}
public static int Fifo(int z)
{
for (x = 0; x < seats.Length; x++)
{
if (seats[x] == seats.Min())
{
//x = z;
break;
}
}
return x;
}
}