日期:2014-05-17 浏览次数:20930 次
...省using
namespace NCYS1
{
class Program
{
private static bool mutex;
private static Mutex mut1 = new Mutex(false, "MyMutex", out Program.mutex);
static void Main(string[] args)
{
ThreadStart ts1 = new ThreadStart(UseResource1);
Thread myThread = new Thread(ts1);
myThread.Name = String.Format("线程{0}", 1);
myThread.Start();
Console.ReadKey();
}
private static void UseResource1()
{
int i=1;
while (true)
{
mut1.WaitOne();
Console.WriteLine("{0}已经进入临界区", Thread.CurrentThread.Name);
Thread.Sleep(1000);
Console.WriteLine("{0}已经离开临界区 {1}\r\n", Thread.CurrentThread.Name,i++);
mut1.ReleaseMutex();
}
}
}
}
...省using
namespace NEICUNYINSHE
{
class Program
{
private static bool mutex;
private static Mutex mut2 = new Mutex(false, "MyMutex", out Program.mutex);
static void Main(string[] args)
{