2009-08-02 16 views
5

एमएसडीएन "Threading Tutorial"
के नमूना उदाहरण 4 से लाइन पर निम्नलिखित कोड त्रुटियों के साथ टिप्पणी की गई है --- --- त्रुटियां यहां --- "हैं।
क्या गलत है?थ्रेडिंग ट्यूटोरियल क्रैश से एमएसडीएन नमूना क्यों करता है?

using System; 
using System.Threading; 

public class MutexSample 
{ 
    static Mutex gM1; 
    static Mutex gM2; 
    const int ITERS = 100; 
    static AutoResetEvent Event1 = new AutoResetEvent(false); 
    static AutoResetEvent Event2 = new AutoResetEvent(false); 
    static AutoResetEvent Event3 = new AutoResetEvent(false); 
    static AutoResetEvent Event4 = new AutoResetEvent(false); 

    public static void Main(String[] args) 
    { 
     Console.WriteLine("Mutex Sample ..."); 
     // Create Mutex initialOwned, with name of "MyMutex". 
     gM1 = new Mutex(true, "MyMutex"); 
     // Create Mutex initialOwned, with no name. 
     gM2 = new Mutex(true); 
     Console.WriteLine(" - Main Owns gM1 and gM2"); 

     AutoResetEvent[] evs = new AutoResetEvent[4]; 
     evs[0] = Event1; // Event for t1 
     evs[1] = Event2; // Event for t2 
     evs[2] = Event3; // Event for t3 
     evs[3] = Event4; // Event for t4 

     MutexSample tm = new MutexSample(); 
     Thread thread1 = new Thread(new ThreadStart(tm.t1Start)); 
     Thread thread2 = new Thread(new ThreadStart(tm.t2Start)); 
     Thread thread3 = new Thread(new ThreadStart(tm.t3Start)); 
     Thread thread4 = new Thread(new ThreadStart(tm.t4Start)); 
     thread1.Start(); // Does Mutex.WaitAll(Mutex[] of gM1 and gM2) 
     thread2.Start(); // Does Mutex.WaitOne(Mutex gM1) 
     thread3.Start(); // Does Mutex.WaitAny(Mutex[] of gM1 and gM2) 
     thread4.Start(); // Does Mutex.WaitOne(Mutex gM2) 

     Thread.Sleep(2000); 
     Console.WriteLine(" - Main releases gM1"); 
     gM1.ReleaseMutex(); // t2 and t3 will end and signal 

     Thread.Sleep(1000); 
     Console.WriteLine(" - Main releases gM2"); 
     gM2.ReleaseMutex(); // t1 and t4 will end and signal 

     // Waiting until all four threads signal that they are done. 
     WaitHandle.WaitAll(evs); 
     Console.WriteLine("... Mutex Sample"); 
    } 

    public void t1Start() 
    { 
     Console.WriteLine("t1Start started, Mutex.WaitAll(Mutex[])"); 
     Mutex[] gMs = new Mutex[2]; 
     gMs[0] = gM1; // Create and load an array of Mutex for WaitAll call 
     gMs[1] = gM2; 
     Mutex.WaitAll(gMs); // Waits until both gM1 and gM2 are released 
     Thread.Sleep(2000); 
     Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[]) satisfied"); 
     Event1.Set();  // AutoResetEvent.Set() flagging method is done 
    } 

    public void t2Start() 
    { 
     Console.WriteLine("t2Start started, gM1.WaitOne()"); 
     gM1.WaitOne(); // Waits until Mutex gM1 is released ---errors is here--- 
     Console.WriteLine("t2Start finished, gM1.WaitOne() satisfied"); 
     Event2.Set();  // AutoResetEvent.Set() flagging method is done 
    } 

    public void t3Start() 
    { 
     Console.WriteLine("t3Start started, Mutex.WaitAny(Mutex[])"); 
     Mutex[] gMs = new Mutex[2]; 
     gMs[0] = gM1; // Create and load an array of Mutex for WaitAny call 
     gMs[1] = gM2; 
     Mutex.WaitAny(gMs); // Waits until either Mutex is released 
     Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])"); 
     Event3.Set();  // AutoResetEvent.Set() flagging method is done 
    } 

    public void t4Start() 
    { 
     Console.WriteLine("t4Start started, gM2.WaitOne()"); 
     gM2.WaitOne(); // Waits until Mutex gM2 is released 
     Console.WriteLine("t4Start finished, gM2.WaitOne()"); 
     Event4.Set(); // AutoResetEvent.Set() flagging method is done 
    } 
} 
+1

के लिए गैर मानसिक, कैसे त्रुटि संदेश पोस्ट करने के बारे ....? –

+0

यहां त्रुटि है {"प्रतीक्षा छोड़ दिया गया म्यूटेक्स के कारण पूरा हुआ।"} सिस्टम पर। थ्रेडिंग। वाइटहैंडल.एटऑन (इंट 64 टाइमआउट, बूलियन एक्ज़िट कॉन्टेक्स्ट) \ r \ n MutexSample.t2 स्टार्ट() में सी: \\ उपयोगकर्ता \ \ billnewhp \\ AppData \\ स्थानीय \\ अस्थायी परियोजनाएं \\ ConsoleApplication1 \\ Program.cs: system 74 \ r \ n System.hreading.ExecutionContext.Run (निष्पादन कॉन्टेक्स्ट निष्पादन कॉन्टेक्स्ट, कॉन्टेक्स्ट कॉलबैक कॉलबैक, ऑब्जेक्ट स्टेट) \ r \ n पर सिस्टम। थ्रेडिंग। थ्रेडहेल्पर। थ्रेडस्टार्ट() " – user149169

+0

यह एमएसडीएन नमूना है, जो टूटा हुआ दिखता है। http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx#vcwlkthreadingtutorialexample4mutex –

उत्तर

6

एक Mutex पर प्रतीक्षा करने के बाद आप इसे जारी करने,

Mutex.ReleaseMutex() 
से पहले धागे बाहर निकलता है

का उपयोग कर सकते है।

तय t1start - t4start

public void t1Start() 
{ 
    Console.WriteLine("t1Start started, Mutex.WaitAll(Mutex[])"); 
    Mutex[] gMs = new Mutex[2]; 
    gMs[0] = gM1; // Create and load an array of Mutex for WaitAll call 
    gMs[1] = gM2; 
    Mutex.WaitAll(gMs); // Waits until both gM1 and gM2 are released 
    Thread.Sleep(2000); 
    Console.WriteLine("t1Start finished, Mutex.WaitAll(Mutex[]) satisfied"); 
    Event1.Set();  // AutoResetEvent.Set() flagging method is done 
    gM1.ReleaseMutex(); 
    gM2.ReleaseMutex(); 
} 
public void t2Start() 
{ 
    Console.WriteLine("t2Start started, gM1.WaitOne()"); 
    gM1.WaitOne(); // Waits until Mutex gM1 is released ---errors is here---  
    Console.WriteLine("t2Start finished, gM1.WaitOne() satisfied"); 

    gM1.ReleaseMutex(); 
    Event2.Set();  // AutoResetEvent.Set() flagging method is done 

} 
public void t3Start() 
{ 
    Console.WriteLine("t3Start started, Mutex.WaitAny(Mutex[])"); 
    Mutex[] gMs = new Mutex[2]; 
    gMs[0] = gM1; // Create and load an array of Mutex for WaitAny call 
    gMs[1] = gM2; 

    int result = Mutex.WaitAny(gMs); // Waits until either Mutex is released 
    gMs[result].ReleaseMutex(); 
    Console.WriteLine("t3Start finished, Mutex.WaitAny(Mutex[])"); Event3.Set();  // AutoResetEvent.Set() flagging method is done 
} 
public void t4Start() 
{ 
    Console.WriteLine("t4Start started, gM2.WaitOne()"); 
    gM2.WaitOne(); // Waits until Mutex gM2 is released 
    Console.WriteLine("t4Start finished, gM2.WaitOne()"); 
    Event4.Set(); // AutoResetEvent.Set() flagging method is done 
    gM2.ReleaseMutex(); 
} 
+0

धन्यवाद, रिलीजम्यूटेक्स() समस्या को हल करें – user149169

+0

+1 आप [5+ बार अपवॉट्स] प्राप्त कर चुके होंगे (http://stackoverflow.com/questions/6464219/why-does-this- नमूना -code-से-माइक्रोसॉफ्ट-दुर्घटना/6464324 # 6464324) साथ ही बाद में डुप्लीज़ को रोक दिया गया था, आपने –

+0

प्रश्न के टैग और शीर्षक को संपादित किया था, इस तरह की शर्म की बात है कि एमएसडीएन ट्यूटोरियल को एसओ पर यहां ठीक किया जाना है। – VSZM

संबंधित मुद्दे