Vla_DOS

MutexStack

Dec 8th, 2022 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. namespace x
  8. {
  9.     public class MyStack
  10.     {
  11.         Mutex mutexObj = new Mutex();
  12.         public int Count { get; set; }
  13.         static Stack<double> stack;
  14.         public MyStack(int Count)
  15.         {
  16.             this.Count = Count;
  17.             Thread AThread = new Thread(new ThreadStart(Input));
  18.             AThread.Name = $"Thread Input";
  19.             Thread BThread = new Thread(new ThreadStart(Checker));
  20.             BThread.Name = $"Thread Checker";
  21.             Thread CThread = new Thread(new ThreadStart(Output));
  22.             CThread.Name = $"Thread Output";
  23.             //start input thread
  24.             AThread.Start();
  25.             Task.Delay(10);
  26.  
  27.             //translate date
  28.             BThread.Start();
  29.             Task.Delay(15);
  30.  
  31.             //output default date
  32.             CThread.Start();
  33.             Task.Delay(10);
  34.         }
  35.  
  36.         public void Input()
  37.         {
  38.             mutexObj.WaitOne();     // зупиняєм потік для отримання мютекса
  39.             Console.WriteLine($"{Thread.CurrentThread.Name} Start");
  40.             stack = new Stack<double>();
  41.             for (int i = 0; i < Count; i++)
  42.             {
  43.                 stack.Push(i);
  44.             }
  45.             mutexObj.ReleaseMutex();    // звільняєм мютекс
  46.  
  47.         }
  48.  
  49.         public void Output()
  50.         {
  51.             mutexObj.WaitOne();     // зупиняєм потік для отримання мютекса
  52.             Console.WriteLine($"{Thread.CurrentThread.Name} Start");
  53.             foreach (double b in stack)
  54.             {
  55.                 Console.Write(b + "\t");
  56.             }
  57.             Console.WriteLine();
  58.             mutexObj.ReleaseMutex();    // // звільняєм мютекс
  59.         }
  60.  
  61.         public void Checker()
  62.         {
  63.             mutexObj.WaitOne();     // зупиняєм потік для отримання мютекса
  64.             Console.WriteLine($"{Thread.CurrentThread.Name} Start");
  65.             double to_check = 3;
  66.             HashSet<double> checker = new HashSet<double>();
  67.             foreach (double b in stack)
  68.                 checker.Add(b);
  69.  
  70.             if (checker.Contains(to_check))
  71.                 Console.WriteLine("Елемент " + to_check + " знайдено!");
  72.             else
  73.                 Console.WriteLine("Елемент " + to_check + " НЕ знайдено!");
  74.  
  75.             mutexObj.ReleaseMutex();    // // звільняєм мютекс
  76.         }
  77.     }
  78.  
  79.     class Program
  80.     {
  81.         static void Main()
  82.         {
  83.             try
  84.             {
  85.                 int size = 10;
  86.                 MyStack myStack = new MyStack(size);
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 Console.WriteLine(ex.Message);
  91.             }
  92.             Console.ReadKey();
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment