-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
82 lines (73 loc) · 2.38 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System.Threading;
using affichage;
namespace refactoring
{
class Program
{
public static Affichage affichage = new Affichage();
public static readonly int[] listVoiture = { 30, 16, 15, 7, 3, 40, 56, 22, 29, 10 };// liste de voiture
public static Thread[] thr = new Thread[listVoiture.Length];
public static Mutex race;
static void Main(string[] args)
{
race = new Mutex();
int[][] classement = new int[3][];
initClassement(classement);
initMultiThread(classement, race);
startMultiThread();
readMemory(classement, race);
}
public static void initClassement(int[][] classement)
{
for (int i = 0; i < 3; i++)
{
classement[i] = new int[] { i, 0, 0, 0, 0, 2, 0, 0, listVoiture[i] };
}
}
public static void initMultiThread(int[][] classement, Mutex sem)
{
Circuit circuit = new Circuit();
for (int i = 0; i < 3; i++)
{
int temp = i;
int idVoiture = classement[i][8];
thr[temp] = new Thread(() => circuit.simu(temp, idVoiture, classement, race));
thr[temp].Name = "Voiture " + temp;
}
}
public static void startMultiThread()
{
for (int i = 0; i < 3; i++)
{
thr[i].Start();
}
Thread.Sleep(500);
}
/********************************** fonctions auxiliaires ******************************************/
public static bool readMemory(int[][] classement, Mutex sem)
{
bool check = true;
while (check)
{
int counter = 0;
for (int i = 0; i < classement.Length; i++)
{
if (classement[i][5] == 0)
{
counter++;
}
if (counter == classement.Length)
{
return false;
}
}
//Console.Clear();
sem.WaitOne();
affichage.affichage(classement);
sem.ReleaseMutex();
Thread.Sleep(2500);
};
return check;
}
}
}