Problem with Mutex in multithreading Application
-
I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )
-
I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )
Try switching your Thread.Sleep(100) with your mutex.Release(). Since Thread.Sleep() is a static method you can't be sure which thread is sleeping if you have released the synchronization Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource.
public class Test
{
protected Mutex mutex;
public Test()
{
mutex = new Mutex(false);
Thread A = new Thread(new ThreadStart(TF));
Thread B = new Thread(new ThreadStart(TF));
A.Start();
B.Start();
}
void TF()
{
while(true)
{
mutex.WaitOne();
Console.Write("do some work {0}", Thread.CurrentThread.Name);
Thread.Sleep(100);
mutex.ReleaseMutex();
}
}
}and since your synchronization is inside an infinite while loop I don't know what will happen as far as sharing process time -- modified at 16:11 Tuesday 22nd November, 2005
-
Try switching your Thread.Sleep(100) with your mutex.Release(). Since Thread.Sleep() is a static method you can't be sure which thread is sleeping if you have released the synchronization Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource.
public class Test
{
protected Mutex mutex;
public Test()
{
mutex = new Mutex(false);
Thread A = new Thread(new ThreadStart(TF));
Thread B = new Thread(new ThreadStart(TF));
A.Start();
B.Start();
}
void TF()
{
while(true)
{
mutex.WaitOne();
Console.Write("do some work {0}", Thread.CurrentThread.Name);
Thread.Sleep(100);
mutex.ReleaseMutex();
}
}
}and since your synchronization is inside an infinite while loop I don't know what will happen as far as sharing process time -- modified at 16:11 Tuesday 22nd November, 2005
Thank you! I'll try to make it static now. BIG Thanks!
-
Try switching your Thread.Sleep(100) with your mutex.Release(). Since Thread.Sleep() is a static method you can't be sure which thread is sleeping if you have released the synchronization Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource.
public class Test
{
protected Mutex mutex;
public Test()
{
mutex = new Mutex(false);
Thread A = new Thread(new ThreadStart(TF));
Thread B = new Thread(new ThreadStart(TF));
A.Start();
B.Start();
}
void TF()
{
while(true)
{
mutex.WaitOne();
Console.Write("do some work {0}", Thread.CurrentThread.Name);
Thread.Sleep(100);
mutex.ReleaseMutex();
}
}
}and since your synchronization is inside an infinite while loop I don't know what will happen as far as sharing process time -- modified at 16:11 Tuesday 22nd November, 2005
whizzs wrote:
Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource
Huh? A threads owns the mutex once the WaitOne function completes execution. And it's absolute *** that the code will work only if the mutex is within one function. The only requirement is that the Mutex object must be shared between the two threads, which the OP code does. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )
Alexandr Sergeevich Ilyin wrote:
but one moment both Threads are waiting for something, for example Console Input or Window Resize.
Could you be a bit more clear? Do you notice just a pause or do the two threads deadlock? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Alexandr Sergeevich Ilyin wrote:
but one moment both Threads are waiting for something, for example Console Input or Window Resize.
Could you be a bit more clear? Do you notice just a pause or do the two threads deadlock? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
It is not deadlock, Just pause. It is enough to run some application or resize window to continue... -------------------------------------------------------------------- In .NET examples Mutex variable is always static, is it Important ? Why ?
-
I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )
Take a look at AutoResetEvent or ManualResetEvent as an alternative to using Mutex. Also looking at your code both your threads go into indefinite wait. one solution is: in TF1 delete the mutex.WaitOne statement. Then replace delete the Thread.Sleep(100) line. As the last statement in TF1, add mutex.WaitOne(). This will execute TF1 first, who writes the message, releases the Mutex and then goes into a Wait for state until signaled by TF2. In this way your threads will take turns. If you need both threads to operate concurrently then you need to add the mutex coordination to the mainline code that started the threads. BTW, you dont need the Thread.Sleep() in TF2 either unless you are just trying to slow down the I/O. Mike