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