I need help with C mutex sample code using System using Sys
I need help with C#
// mutex sample code using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
class Test {
// Create a new Mutex. The creating thread owns the
// Mutex.
private static Mutex mut = new Mutex(true);
private const int numIterations = 1;
private const int numThreads = 2;
static void Main()
{
// Create the threads that will use the protected resource.
for (int i = 0; i < numThreads; i++) {
Thread myThread = new Thread(newThreadStart(MyThreadProc));
myThread.Name =String.Format(\"Thread{0}\", i + 1);
myThread.Start();
}
// Wait one second before allowing other threads to
// acquire the Mutex.
Console.WriteLine(\"Creating thread owns the Mutex.\");
Thread.Sleep(1000);
Console.WriteLine(\"Creating thread releases the Mutex.\ \ \");
mut.ReleaseMutex();
//Console.ReadKey();
}
private static void MyThreadProc()
{
for (int i = 0; i < numIterations; i++)
{
UseResource();
}
}
// This method represents a resource that must be synchronized
// so that only one thread at a time can enter.
private static void UseResource()
{
// Wait until it is safe to enter.
mut.WaitOne();
Console.WriteLine(\"{0} has entered the protected area\",
Thread.CurrentThread.Name);
// Place code to access non-reentrant resources here.
// do work here!!
Thread.Sleep(500);
Console.WriteLine(\"{0} is leaving the protected area\ \ \",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
}
}
Part I 1. The following is the sample code that shows two threads use mutex to communicate. Note mutex is a simplified semaphore. 1 of 4 Refer to this piece of sam code, write a program that conduct the ple followings (1) It contains two thread, t1 and t2 (2) t1 and t2 share a variable, myNum; (3) When my Nun is empty use a to show empty), t1 will generate a random number between 1 and 99, and save to myNum; (4) When my Nun is not empty, t2 will get this number and save it to an array, myNumArray, in ascending order. (5) After t2 insert a new number in myNunArray, it also shows the result mutex sample code using System using System-Collections.Generic; using System-Linq using System-Text using System. Threading; class Test Create a new Mutex The creating thread owns the Mutex private static Mutex nut new Mutex(true private const int nun erations 1; private const int nunThreads 2; static void Main Create the threads that will use the protected resource. for (int i 0 i numThreads i++ Thread my Thread new Thread (new ThreadStart My Thread Proc Thread i 1 Thread Name String Format Thread-Star Wait one second before allowing other threads to acquire the Mutex Console Write Creating thread owns the Mutex Thread.Sleep(1000 Console Write thread releases the Mutex.Vr\ mutt ReleaseMutex //Console ReadKey(); private static void MyThreadProc() for (int i numIterait ons; it e; i UseResource() This method represents a resource that nust be synchronized so that only one thread at a time can enter private static void seResource Wait until it is safe to enter. mut.Waitone(); Console. has entered the protected area WriteLine(\"(e) Thread CurrentThread. Nane Place code to access non-reentrant resources here. do work here Sleep(See Console WriteLine is leaving the protected areaVr\ \", Thread CurrentThread.Name Release the Mutex mut.ReleaseMutexSolution
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
class Test {
// Create a new Mutex. The creating thread owns the
// Mutex.
private static Mutex mut = new Mutex(true);
private const int numIterations = 1;
private const int numThreads = 2;
static void Main()
{
// Create the threads that will use the protected resource.
for (int i = 0; i < numThreads; i++) {
Thread myThread = new Thread(newThreadStart(MyThreadProc));
myThread.Name =String.Format(\"Thread{0}\", i + 1);
myThread.Start();
}
// Wait one second before allowing other threads to
// acquire the Mutex.
Console.WriteLine(\"Creating thread owns the Mutex.\");
Thread.Sleep(1000);
Console.WriteLine(\"Creating thread releases the Mutex.\ \ \");
mut.ReleaseMutex();
//Console.ReadKey();
}
private static void MyThreadProc()
{
for (int i = 0; i < numIterations; i++)
{
UseResource();
}
}
// This method represents a resource that must be synchronized
// so that only one thread at a time can enter.
private static void UseResource()
{
// Wait until it is safe to enter.
mut.WaitOne();
Console.WriteLine(\"{0} has entered the protected area\",
Thread.CurrentThread.Name);
// Place code to access non-reentrant resources here.
// do work here!!
Thread.Sleep(500);
Console.WriteLine(\"{0} is leaving the protected area\ \ \",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
}
}



