jeudi 17 avril 2014

.net - c# multi-threading et Mutex : pas d'attente sortie - Stack Overflow


I'm trying to use Mutex to have with a shared information between 3 threads.


Here is the shared class :


public class SharedMemory
{
public Mutex mutex;
protected static int sharedInfo;

public SharedMemory()
{
mutex = new Mutex(false);
sharedInfo = 0;
}

public void StartProcessing()
{
// Init objects
AlertSender sender1 = new AlertSender("T1", 12, 5);
AlertSender sender2 = new AlertSender("T2", 30, 5);
AlertSender sender3 = new AlertSender("T3", 5, 8);

// Init Threads
List<Thread> threads = new List<Thread>();

Thread temp;
temp = new Thread(new ThreadStart(sender1.BetaTest));
temp.Name = "T1";
threads.Add(temp);
temp = new Thread(new ThreadStart(sender2.BetaTest));
temp.Name = "T2";
threads.Add(temp);
temp = new Thread(new ThreadStart(sender3.BetaTest));
temp.Name = "T3";
threads.Add(temp);

foreach (Thread t in threads)
{
Console.WriteLine(string.Format("Thread '{0}' - pId : {1}", t.Name, t.ManagedThreadId));
}

foreach (Thread t in threads)
{
t.Start();
}

Console.WriteLine("Main program has finished !");
Console.WriteLine("Press ENTER to end...");
Console.ReadLine();

foreach (Thread t in threads)
{
t.Abort();
}
}
}

Then here is the Thread 'AlertSender' code :


class AlertSender : SharedMemory 
{
private string name;
private int maxLimit;
private int delay;

public AlertSender(string name, int maxLimit, int delay)
{
this.name = name;
this.maxLimit = maxLimit;
this.delay = delay;
}

public void BetaTest()
{
Console.WriteLine(string.Format("Thread '{0}' : START", this.name));

while (sharedInfo < maxLimit)
{
Console.WriteLine(string.Format("'{0}' : WAITING", this.name));
mutex.WaitOne();

try
{
sharedInfo++;
Console.WriteLine(string.Format("'{0}' HAS THE POWER : {1}. Waiting for {2} seconds...", this.name, sharedInfo, delay));

Thread.Sleep(delay * 1000);
}
finally
{
Console.WriteLine(string.Format("'{0}' : RELEASE", this.name));
mutex.ReleaseMutex();
}
}

Console.WriteLine(string.Format("Thread '{0}' : STOP", this.name));
}
}

When I run this code, threads are increasing the "SharedInfo" even if the previous thread has not done the "ReleaseMutex" yet...


Any idea of what I am doind wrong ?


Thanks for your feedback !



I'm trying to use Mutex to have with a shared information between 3 threads.


Here is the shared class :


public class SharedMemory
{
public Mutex mutex;
protected static int sharedInfo;

public SharedMemory()
{
mutex = new Mutex(false);
sharedInfo = 0;
}

public void StartProcessing()
{
// Init objects
AlertSender sender1 = new AlertSender("T1", 12, 5);
AlertSender sender2 = new AlertSender("T2", 30, 5);
AlertSender sender3 = new AlertSender("T3", 5, 8);

// Init Threads
List<Thread> threads = new List<Thread>();

Thread temp;
temp = new Thread(new ThreadStart(sender1.BetaTest));
temp.Name = "T1";
threads.Add(temp);
temp = new Thread(new ThreadStart(sender2.BetaTest));
temp.Name = "T2";
threads.Add(temp);
temp = new Thread(new ThreadStart(sender3.BetaTest));
temp.Name = "T3";
threads.Add(temp);

foreach (Thread t in threads)
{
Console.WriteLine(string.Format("Thread '{0}' - pId : {1}", t.Name, t.ManagedThreadId));
}

foreach (Thread t in threads)
{
t.Start();
}

Console.WriteLine("Main program has finished !");
Console.WriteLine("Press ENTER to end...");
Console.ReadLine();

foreach (Thread t in threads)
{
t.Abort();
}
}
}

Then here is the Thread 'AlertSender' code :


class AlertSender : SharedMemory 
{
private string name;
private int maxLimit;
private int delay;

public AlertSender(string name, int maxLimit, int delay)
{
this.name = name;
this.maxLimit = maxLimit;
this.delay = delay;
}

public void BetaTest()
{
Console.WriteLine(string.Format("Thread '{0}' : START", this.name));

while (sharedInfo < maxLimit)
{
Console.WriteLine(string.Format("'{0}' : WAITING", this.name));
mutex.WaitOne();

try
{
sharedInfo++;
Console.WriteLine(string.Format("'{0}' HAS THE POWER : {1}. Waiting for {2} seconds...", this.name, sharedInfo, delay));

Thread.Sleep(delay * 1000);
}
finally
{
Console.WriteLine(string.Format("'{0}' : RELEASE", this.name));
mutex.ReleaseMutex();
}
}

Console.WriteLine(string.Format("Thread '{0}' : STOP", this.name));
}
}

When I run this code, threads are increasing the "SharedInfo" even if the previous thread has not done the "ReleaseMutex" yet...


Any idea of what I am doind wrong ?


Thanks for your feedback !


0 commentaires:

Enregistrer un commentaire