public void firstPart() { int divisations = n / NR_MAXIM; Console.WriteLine("n=" + n + " NR_MAXIM=" + NR_MAXIM + " divisations = " + divisations); int i = 0; for (i = 0; i < NR_MAXIM - 1; i++) { matrixLimits ml = new matrixLimits(); ml.begin = i * divisations + 1; ml.end = (i + 1) * divisations; ml.threadNumber = i + 1; // Console.WriteLine("thread-ul " + ml.threadNumber + " (" + ml.begin + "," + ml.end + ")"); firstThreads[i] = new Thread(new ParameterizedThreadStart(RoyFloyd)); firstThreads[i].Start(ml); } matrixLimits lastMl = new matrixLimits(); lastMl.begin = i * divisations + 1; lastMl.end = n; lastMl.threadNumber = i + 1; firstThreads[i] = new Thread(new ParameterizedThreadStart(RoyFloyd)); firstThreads[i].Start(lastMl); // p.RoyFloyd(); /* * for (int k = 0; k <= i; k++) * firstThreads[k].Join(); */ int currentValue; notr.WaitOne(); currentValue = numberOfThreadsReady; notr.ReleaseMutex(); while (currentValue < i) { notr.WaitOne(); currentValue = numberOfThreadsReady; notr.ReleaseMutex(); } //displayMatrix(); Console.WriteLine("Comparari: " + comparari); }
void RoyFloyd(object obj) { matrixLimits ml = (matrixLimits)obj; Console.WriteLine("intra thread-ul " + ml.threadNumber + " (" + ml.begin + ", " + ml.end + ")"); Thread.Sleep(1200); int currentStart = ml.begin; int currentEnd = ml.end; Boolean finished = false; while (finished == false) { for (int k = 1; k <= n && finished == false; k++) { for (int i = ml.begin; i <= ml.end && finished == false; i++) { for (int j = currentStart; j <= currentEnd; j++) { mutex4.WaitOne(); comparari++; mutex4.ReleaseMutex(); if (cost[i][j] > cost[i][k] + cost[k][j]) { cost[i][j] = cost[i][k] + cost[k][j]; } } } } //the thread is ready to go forward. Increase the index. Console.WriteLine("dupa for thread-ul" + ml.threadNumber); mutex2.WaitOne(); int currentReady = ++readyToGo; Console.WriteLine("Thread-ul " + ml.threadNumber + " face readyToGo " + currentReady + " maxim e " + NR_MAXIM); mutex2.ReleaseMutex(); //wait untill all the threads are ready to go. while (currentReady < NR_MAXIM) { //Thread.Sleep(100); mutex2.WaitOne(); currentReady = readyToGo; // Console.WriteLine(currentReady); mutex2.ReleaseMutex(); } mutex3.WaitOne(); mod++; if (mod % NR_MAXIM == 0 && mod > 0) { mutex2.WaitOne(); readyToGo = 0; mutex2.ReleaseMutex(); } mutex3.ReleaseMutex(); Console.WriteLine("Thread-ul" + ml.threadNumber + " face readyToGo 0"); updateLimits(ref currentStart, ref currentEnd); Console.WriteLine("++++++++++++++Thread-ul " + ml.threadNumber + "(" + currentStart + "," + currentEnd + ")"); if (currentStart == ml.begin && currentEnd == ml.end) { finished = true; } } notr.WaitOne(); numberOfThreadsReady++; Console.WriteLine("Number of threads ready for the second part: " + numberOfThreadsReady); notr.ReleaseMutex(); lock (startPartTwo) { Monitor.Wait(startPartTwo); } Console.WriteLine("Thread-ul " + ml.threadNumber + " trece la partea a 2-a"); Consume(ml.threadNumber); }