using System;
using System.Threading;
namespace MutexConsoleApp
{
class Program
{
public static Mutex mut = new Mutex(false,null);
static int N = 100;
static private Thread th;
static private Thread tw;
static void Main(string[] args)
{
            th = new Thread(new ThreadStart(f));
tw = new Thread(new ThreadStart(fun));
th.Start();
tw.Start();
}
static void f()
{
while(N>0)
{
mut.WaitOne();
Console.WriteLine("one{0}",N--);
mut.ReleaseMutex();
}
}
static void fun()
{
while(N>0)
{
mut.WaitOne();
Console.WriteLine("two{0}",N--);
mut.ReleaseMutex();
}
}

}
}
我要的结果是
one100
two99
one98
two97
....但是却得不到,,这是怎么回事啊,高手帮帮忙,,,麻烦大家就不要去复制MSDN和去网上查找这类的东西来粘贴了,,基本那些我都看过了,但是还是看不懂啊,

解决方案 »

  1.   

    这样就差不多了:    class Program
        {
            public static Mutex mut = new Mutex(false, null);
            static int N = 100;
            static private Thread th;
            static private Thread tw;        static void Main(string[] args)
            {
                th = new Thread(new ThreadStart(f));
                tw = new Thread(new ThreadStart(fun));
                th.Start();
                tw.Start();            Console.ReadLine();
            }
            static void f()
            {
                while (N > 0)
                {
                    mut.WaitOne();                Console.WriteLine("one{0}", N--);
                    //等待一下
                    System.Threading.Thread.Sleep(100); 
                    mut.ReleaseMutex();
                }
            }
            static void fun()
            {
                while (N > 0)
                {
                    mut.WaitOne();
                    Console.WriteLine("two{0}", N--);
                    //等待一下
                    System.Threading.Thread.Sleep(100);
                    mut.ReleaseMutex();
                }
            }    }
      

  2.   

    也有一种不用Sleep的方法,但不是用Mutex ,需要用Monitor.Wait和Monitor.PulseAll,象下边这样:  class Program
        {
            //public static Mutex mut = new Mutex(false, null);
            static int N = 100;
            static private Thread th;
            static private Thread tw;
            public static Object _synObject = new object();
            static void Main(string[] args)
            {
                th = new Thread(new ThreadStart(f));
                tw = new Thread(new ThreadStart(fun));
                th.Start();
                tw.Start();            Console.ReadLine();
            }              static void f()
            {
                while (N > 0)
                {
                    lock (_synObject)
                    {
                        Console.WriteLine("one{0}", N--);
                        Monitor.PulseAll(_synObject);
                        Monitor.Wait(_synObject);        
                    }
                }
            }
            static void fun()
            {
                while (N > 0)
                {
                    lock (_synObject)
                    {
                        Console.WriteLine("Two{0}", N--);
                        Monitor.PulseAll(_synObject);
                        Monitor.Wait(_synObject);                }            }
            }    }
      

  3.   

    为什么要等待一下呢?
      class Program
        {
            public static Mutex mut = new Mutex(false, null);
            static int N = 100;
            static private Thread th;
                 static void Main(string[] args)
            {
                th = new Thread(new ThreadStart(f));            th.Start();
                           Console.ReadLine();
            }
            static void f()
            {
                while (N > 0)
                {
                    mut.WaitOne();                Console.WriteLine("one{0}", N--);
                  
                    
                }
            }我这养写,没有mut.ReleaseMutex();
    他也不会出错,到底是怎么回事呀,高手解释下好吗?
      

  4.   

    但是却得不到,,这是怎么回事啊
    ------------------------------原因是当一个线程ReleaseMutex()的时候,你的代码并不能保证另外一个线程已经在WaitOne的状态等待,但是如果加了一个Sleep,就能在很大程度上(也并非100%)保证上让另一个线程处于WaitOne的状态.我的第二个做法是100%保证一个线程放开锁的时候一定是另一个线程(而不是自己)得到锁,这是由Monitor.Wait和Monitor.PulseAll的机制保证的.
      

  5.   

    RedGoldFish(红金鱼)
    怎么我运行你写的那个代码,更离谱啊,,基本没出现几个two,,都是one哦.
      

  6.   

    不是,用的Mutex的,,Monitor.Wait的可以
      

  7.   

    不是,用的Mutex的,,Monitor.Wait的可以
    ---------------------------------------把Sleep时间沿长些试试,比如Sleep(500). 我上边说了,加个Sleep能在很大程度上(也并非100%)让另一个线程处于WaitOne的状态. 在我的机器上Sleep(100)都能达到你那个One,Two交错的要求.说实话,想达到你那个要求,Mutex不是最好选择,因为当一个线程放Mutex的时候(用ReleaseMutex)它在机制上并不能保证另一个线程优先与自己得到Mutex.
      

  8.   

    但是用Monitor.Wait和Monitor.PulseAll就不同:在一个线程用Monitor.Wait放开锁之前,它用Monitor.PulseAll保证把另一个线程推到等待队列的最前边,这样等它一放开锁(用Monitor.Wait),另一个线程一定会优先得到锁.