Объясните,как работает мьютекс на примере кода: - C#
Формулировка задачи:
Вот код:
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Lesson_1
- {
- class Program
- {
- static Mutex mutexObj = new Mutex();
- static int x = 0;
- static void Main(string[] args)
- {
- for(int i = 0;i<5;i++)
- {
- Thread myThread = new Thread(Count);
- myThread.Name = "Поток: " + i.ToString();
- myThread.Start();
- Console.WriteLine("{0}:{1}", myThread.Name, myThread.IsBackground);
- }
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- }
- public static void Count()
- {
- mutexObj.WaitOne();
- x = 1;
- for(int i = 0;i<9;i++)
- {
- Console.WriteLine("{0}: {1}", Thread.CurrentThread.Name, i);
- x++;
- Thread.Sleep(100);
- }
- mutexObj.ReleaseMutex();
- }
- }
- }
Решение задачи: «Объясните,как работает мьютекс на примере кода:»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Lesson_5
- {
- class Program
- {
- volatile static int x = 0;
- static Mutex mutex = new Mutex();
- static void Main(string[] args)
- {
- Thread thread_f = new Thread(Count);
- thread_f.Start();
- for(x = 0;x<10;x++)
- {
- Console.WriteLine(x + ":f");
- }
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey(true);
- }
- static void Count()
- {
- mutex.WaitOne();
- for(x = 0;x<10;x++)
- {
- Console.WriteLine(x+":d");
- }
- mutex.ReleaseMutex();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д