// class constructor // public MutexHandler(bool case1, Mutual_Exclusion_Form form) { // if true, then use case 1 operation // // Ricart & Agrawalas mutual exclusion algorithm, one person on bridge at a time. Everyone eventually allowed to cross (via queue) // parentForm = form; populateHandles(case1); // Create handles data structure, needed for all operational cases // this.setInitialData(); // Sets inital thread data in the shared memory // // Common init code between case 1 and case 2 // parentForm.rInsert(Color.Red); parentForm.rInsert(Color.Red); parentForm.lInsert(Color.Blue); parentForm.lInsert(Color.Blue); // Specific case code execution // if (case1) { // FOR CASE 1 -> EXECUTE MUTUAL EXCLUSION ALGORITHM // Richart_Agrawala(form); } // if false, design protocol 2 states that bridge crossings allowed directionally in sync, but no job indefinately prevented from crossing else { // FOR CASE 2 -> EXECUTE MUTUAL EXCLUSION ALGORITHM // Multi_Bridge(form); } }
public void Multi_Bridge(Mutual_Exclusion_Form form) { workerThreads[0] = new Thread(() => Multi_Thread(0)); workerThreads[1] = new Thread(() => Multi_Thread(1)); workerThreads[2] = new Thread(() => Multi_Thread(2)); workerThreads[3] = new Thread(() => Multi_Thread(3)); foreach (Thread worker in workerThreads) { worker.Start(); } }
public void Richart_Agrawala(Mutual_Exclusion_Form form) { workerThreads[0] = new Thread(() => Richart_Thread(0)); Thread.Sleep(10); workerThreads[1] = new Thread(() => Richart_Thread(1)); Thread.Sleep(15); workerThreads[2] = new Thread(() => Richart_Thread(2)); Thread.Sleep(18); workerThreads[3] = new Thread(() => Richart_Thread(3)); foreach (Thread worker in workerThreads) { worker.Start(); } }