<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FThread%2FMutex</id>
		<title>Csharp/CSharp Tutorial/Thread/Mutex - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FThread%2FMutex"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Mutex&amp;action=history"/>
		<updated>2026-04-29T17:14:52Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Mutex&amp;diff=6656&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Mutex&amp;diff=6656&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Mutex&amp;diff=6657&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Thread/Mutex&amp;diff=6657&amp;oldid=prev"/>
				<updated>2010-05-26T12:20:11Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Name a Mutex==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass {&lt;br /&gt;
&lt;br /&gt;
  public static void Main( string[] args ) {&lt;br /&gt;
    string mutexName = &amp;quot;MainClass&amp;quot;;&lt;br /&gt;
    Mutex m = new Mutex( false, mutexName );&lt;br /&gt;
    for( ;; ) {&lt;br /&gt;
      m.WaitOne( );&lt;br /&gt;
      Console.WriteLine(&amp;quot;Have Mutex&amp;quot;);&lt;br /&gt;
      Console.WriteLine(&amp;quot;Releasing&amp;quot;);&lt;br /&gt;
      m.ReleaseMutex( );&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Have Mutex&lt;br /&gt;
Releasing&lt;br /&gt;
Have Mutex&lt;br /&gt;
Releasing&lt;br /&gt;
...&lt;br /&gt;
...&lt;br /&gt;
^CTerminate batch job (Y/N)? n&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Own a Mutex==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        bool ownsMutex;&lt;br /&gt;
        using (Mutex mutex =new Mutex(true, &amp;quot;MutexExample&amp;quot;, out ownsMutex))&lt;br /&gt;
        {&lt;br /&gt;
            if (ownsMutex)&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Owned&amp;quot;);&lt;br /&gt;
                mutex.ReleaseMutex();&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                Console.WriteLine(&amp;quot;Another instance of this application &amp;quot; +&lt;br /&gt;
                    &amp;quot; already owns the mutex named MutexExample.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Owned&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Threading with Mutex==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass &lt;br /&gt;
{&lt;br /&gt;
  public static ArrayList MyList = new ArrayList();&lt;br /&gt;
  private static Mutex MyMutex = new Mutex(false, &amp;quot;MyMutex&amp;quot;);&lt;br /&gt;
  static void Main(string[] args)&lt;br /&gt;
  {&lt;br /&gt;
      Thread ThreadOne = new Thread(new ThreadStart(MutexExample));&lt;br /&gt;
    ThreadOne.Start();&lt;br /&gt;
  }&lt;br /&gt;
  protected static void MutexExample()&lt;br /&gt;
  {&lt;br /&gt;
    MyMutex.WaitOne();&lt;br /&gt;
    MyList.Add(&amp;quot; a value&amp;quot;);&lt;br /&gt;
    MyMutex.ReleaseMutex();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a Mutex to control a shared resource against two current threads==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;  &lt;br /&gt;
using System.Threading;  &lt;br /&gt;
 &lt;br /&gt;
class MyCounter { &lt;br /&gt;
  public static int count = 0; &lt;br /&gt;
  public static Mutex MuTexLock = new Mutex(); &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
class IncThread {  &lt;br /&gt;
  public Thread thrd;  &lt;br /&gt;
  &lt;br /&gt;
  public IncThread() {  &lt;br /&gt;
    thrd = new Thread(this.run);  &lt;br /&gt;
    thrd.Start();  &lt;br /&gt;
  }  &lt;br /&gt;
  &lt;br /&gt;
  void run() {  &lt;br /&gt;
    Console.WriteLine(&amp;quot;IncThread is waiting for the mutex.&amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
    MyCounter.MuTexLock.WaitOne(); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;IncThread acquires the mutex.&amp;quot;); &lt;br /&gt;
    &lt;br /&gt;
    int num = 10;&lt;br /&gt;
    do {  &lt;br /&gt;
      Thread.Sleep(50);  &lt;br /&gt;
      MyCounter.count++;  &lt;br /&gt;
      Console.WriteLine(&amp;quot;In IncThread, MyCounter.count is &amp;quot; + MyCounter.count);  &lt;br /&gt;
      num--; &lt;br /&gt;
    } while(num &amp;gt; 0);  &lt;br /&gt;
  &lt;br /&gt;
    Console.WriteLine(&amp;quot;IncThread releases the mutex.&amp;quot;);   &lt;br /&gt;
 &lt;br /&gt;
    MyCounter.MuTexLock.ReleaseMutex(); &lt;br /&gt;
  }  &lt;br /&gt;
}  &lt;br /&gt;
 &lt;br /&gt;
class DecThread {  &lt;br /&gt;
  public Thread thrd;  &lt;br /&gt;
  &lt;br /&gt;
  public DecThread() {  &lt;br /&gt;
    thrd = new Thread(new ThreadStart(this.run));  &lt;br /&gt;
    thrd.Start();  &lt;br /&gt;
  }  &lt;br /&gt;
  &lt;br /&gt;
  void run() {  &lt;br /&gt;
    Console.WriteLine(&amp;quot;DecThread is waiting for the mutex.&amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
    MyCounter.MuTexLock.WaitOne(); &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;DecThread acquires the mutex.&amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
    int num = 10;&lt;br /&gt;
    do {  &lt;br /&gt;
      Thread.Sleep(50);  &lt;br /&gt;
      MyCounter.count--;  &lt;br /&gt;
      Console.WriteLine(&amp;quot;In DecThread, MyCounter.count is &amp;quot; + MyCounter.count);  &lt;br /&gt;
      num--; &lt;br /&gt;
    } while(num &amp;gt; 0);  &lt;br /&gt;
  &lt;br /&gt;
    Console.WriteLine(&amp;quot;DecThread releases the mutex.&amp;quot;);  &lt;br /&gt;
 &lt;br /&gt;
    MyCounter.MuTexLock.ReleaseMutex(); &lt;br /&gt;
  }  &lt;br /&gt;
}  &lt;br /&gt;
  &lt;br /&gt;
class MainClass {  &lt;br /&gt;
  public static void Main() {  &lt;br /&gt;
    IncThread mt1 = new IncThread();  &lt;br /&gt;
    DecThread mt2 = new DecThread();  &lt;br /&gt;
  &lt;br /&gt;
    mt1.thrd.Join(); &lt;br /&gt;
    mt2.thrd.Join(); &lt;br /&gt;
  }  &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;IncThread is waiting for the mutex.&lt;br /&gt;
IncThread acquires the mutex.&lt;br /&gt;
DecThread is waiting for the mutex.&lt;br /&gt;
In IncThread, MyCounter.count is 1&lt;br /&gt;
In IncThread, MyCounter.count is 2&lt;br /&gt;
In IncThread, MyCounter.count is 3&lt;br /&gt;
In IncThread, MyCounter.count is 4&lt;br /&gt;
In IncThread, MyCounter.count is 5&lt;br /&gt;
In IncThread, MyCounter.count is 6&lt;br /&gt;
In IncThread, MyCounter.count is 7&lt;br /&gt;
In IncThread, MyCounter.count is 8&lt;br /&gt;
In IncThread, MyCounter.count is 9&lt;br /&gt;
In IncThread, MyCounter.count is 10&lt;br /&gt;
IncThread releases the mutex.&lt;br /&gt;
DecThread acquires the mutex.&lt;br /&gt;
In DecThread, MyCounter.count is 9&lt;br /&gt;
In DecThread, MyCounter.count is 8&lt;br /&gt;
In DecThread, MyCounter.count is 7&lt;br /&gt;
In DecThread, MyCounter.count is 6&lt;br /&gt;
In DecThread, MyCounter.count is 5&lt;br /&gt;
In DecThread, MyCounter.count is 4&lt;br /&gt;
In DecThread, MyCounter.count is 3&lt;br /&gt;
In DecThread, MyCounter.count is 2&lt;br /&gt;
In DecThread, MyCounter.count is 1&lt;br /&gt;
In DecThread, MyCounter.count is 0&lt;br /&gt;
DecThread releases the mutex.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use the Mutex object: WaitOne==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Threading;&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  private static int Runs = 0;&lt;br /&gt;
  static Mutex mtx = new Mutex(false, &amp;quot;RunsMutex&amp;quot;);&lt;br /&gt;
  public static void CountUp() &lt;br /&gt;
  {&lt;br /&gt;
    while (Runs &amp;lt; 10)&lt;br /&gt;
    {&lt;br /&gt;
      // acquire the mutex&lt;br /&gt;
      mtx.WaitOne();&lt;br /&gt;
      int Temp = Runs;&lt;br /&gt;
      Temp++;&lt;br /&gt;
      Console.WriteLine(Thread.CurrentThread.Name + &amp;quot; &amp;quot; + Temp);&lt;br /&gt;
      Thread.Sleep(1000);&lt;br /&gt;
      Runs = Temp;&lt;br /&gt;
      // release the mutex&lt;br /&gt;
      mtx.ReleaseMutex();&lt;br /&gt;
    } &lt;br /&gt;
  }&lt;br /&gt;
  public static void Main() &lt;br /&gt;
  {&lt;br /&gt;
    Thread t2 = new Thread(new ThreadStart(CountUp));&lt;br /&gt;
    t2.Name = &amp;quot;t2&amp;quot;;&lt;br /&gt;
    Thread t3 = new Thread(new ThreadStart(CountUp));&lt;br /&gt;
    t3.Name = &amp;quot;t3&amp;quot;;&lt;br /&gt;
    t2.Start();&lt;br /&gt;
    t3.Start();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;t2 1&lt;br /&gt;
t3 2&lt;br /&gt;
t3 3&lt;br /&gt;
t3 4&lt;br /&gt;
t3 5&lt;br /&gt;
t3 6&lt;br /&gt;
t3 7&lt;br /&gt;
t3 8&lt;br /&gt;
t3 9&lt;br /&gt;
t3 10&lt;br /&gt;
t2 11&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>