Advertisement
ivorakitin

mutex

Apr 9th, 2025
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApp22
  10. {
  11.     class Program
  12.     {
  13.         [MethodImpl (MethodImplOptions.Synchronized)]
  14.         public void DoSomething()
  15.         {
  16.  
  17.         }
  18.         static void Main(string[] args)
  19.         {
  20.             MyCounter c = new MyCounter();
  21.             IncThread myt1 = new IncThread(c);
  22.             DecThread myt2 = new DecThread(c);
  23.             myt1.th.Join();
  24.             myt2.th.Join();
  25.             Console.Read();
  26.         }
  27.     }
  28.     class MyCounter
  29.     {
  30.         private int count = 0;
  31.         private Mutex MuTexLock;
  32.         public MyCounter()
  33.         {
  34.             MuTexLock = new Mutex();
  35.         }
  36.  
  37.         public Mutex getMutex()
  38.         {
  39.             return MuTexLock;
  40.         }
  41.  
  42.         public int getCount()
  43.         {
  44.             return count;
  45.         }
  46.  
  47.         public void setCount(int c)
  48.         {
  49.             count = c;
  50.         }
  51.  
  52.     }
  53.     class IncThread
  54.     {
  55.         public Thread th;
  56.         private MyCounter c;
  57.         public IncThread(MyCounter c)
  58.         {
  59.             this.c = c;
  60.             th = new Thread(this.Go);
  61.             th.Start();
  62.         }
  63.         void Go()
  64.         {
  65.             Console.WriteLine("IncThread is waiting for the mutex.");
  66.             c.getMutex().WaitOne();
  67.             Console.WriteLine("IncThread acquires the mutex.");
  68.             int num = 10;
  69.             do
  70.             {
  71.                 Thread.Sleep(50);
  72.                 c.setCount(c.getCount()+1);
  73.                 Console.WriteLine("In IncThread, MyCounter.count is " + c.getCount());
  74.                 num--;
  75.             } while (num > 0);
  76.             Console.WriteLine("IncThread releases the mutex.");
  77.             c.getMutex().ReleaseMutex();
  78.         }
  79.     }
  80.     class DecThread
  81.     {
  82.         public Thread th;
  83.         private MyCounter c;
  84.         public DecThread(MyCounter c)
  85.         {
  86.             this.c = c;
  87.             th = new Thread(this.Go);
  88.             th.Start();
  89.         }
  90.        void Go()
  91.        {  
  92.             Console.WriteLine("DecThread is waiting for the mutex.");
  93.             c.getMutex().WaitOne();  
  94.             Console.WriteLine("DecThread acquires the mutex.");  
  95.             int num = 10;  
  96.             do  
  97.             {  
  98.                 Thread.Sleep(50);
  99.                 c.setCount(c.getCount() - 1);
  100.                 Console.WriteLine("In DecThread, MyCounter.count is " + c.getCount());  
  101.                 num--;  
  102.             } while (num > 0);  
  103.             Console.WriteLine("DecThread releases the mutex.");
  104.             c.getMutex().ReleaseMutex();  
  105.        }  
  106.     }  
  107. }
  108.  
Tags: Mutex
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement