Задача следующая
Из главного потока запускается дополнительный зацикленный поток. На него ставлю мьютекс.
Создается еще три потока который в момент слипа дополнительного потока должны забрать управление на себя.
Вот пример:
using System;
using System.Threading;
class Test
{
// Create a new Mutex. The creating thread does not own the
// Mutex.
private static Mutex mut;// = new Mutex();
private const int numIterations = 1;
private const int numThreads = 3;
static void Main()
{
Thread ScanThread = new Thread(new ThreadStart(ScanThreadFunc));
ScanThread.Name = "ScanThread";
ScanThread.Start();
// Create the threads that will use the protected resource.
for (int i = 0; i < numThreads; i++)
{
Thread myThread = new Thread(new ThreadStart(MyThreadProc));
myThread.Name = String.Format("Thread{0}", i + 1);
myThread.Start();
}
// The main thread exits, but the application continues to
// run until all foreground threads have exited.
}
private static bool _continue = true;
private static void ScanThreadFunc()
{
mut = new Mutex();
while (_continue)
{
for (int i = 0; i < numIterations; i++)
{
Console.WriteLine("{0} working now \r\n",
Thread.CurrentThread.Name);
}
Thread.Sleep(1000);
}
}
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.
// Simulate some work.
Thread.Sleep(5000);
Console.WriteLine("{0} is leaving the protected area\r\n",
Thread.CurrentThread.Name);
// Release the Mutex.
mut.ReleaseMutex();
}
}
В рез-те получаю
ScanThread working now
Thread1 has entered the protected area
ScanThread working now
ScanThread working now
ScanThread working now
ScanThread working now
Thread1 is leaving the protected area
и т.д.
Т.е. надо чтобы когда Thread1 забирал мьтекс мне надо чтобы он затыкал ScanThread
В чем я не прав?