Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- namespace Muteksy
- {
- class Program
- {
- static List<Thread> watki = new List<Thread>();
- static readonly int liczbaIteracji = 10000;
- static readonly int liczbaWatkow = 100;
- static readonly object _lock = new object();
- static Mutex mutex = new Mutex(false, "tu.kielce.pl mutex");
- static Semaphore sem = new Semaphore(1,1);
- static int licznik = 0;
- static void Main(string[] args)
- {
- Stopwatch stopWatch = new Stopwatch();
- for (int i = 0; i < liczbaWatkow; i++)
- {
- var watek = new Thread((o) =>
- {
- int nrWatku = (int)o;
- for (int j = 0; j < liczbaIteracji; j++)
- {
- /*
- stopWatch.Start();
- licznik++;
- stopWatch.Stop();
- */
- /*
- stopWatch.Start();
- lock(_lock){
- licznik++;
- }
- stopWatch.Stop();
- */
- /*
- stopWatch.Start();
- mutex.WaitOne();
- licznik++;
- mutex.ReleaseMutex();
- stopWatch.Stop();*/
- stopWatch.Start();
- sem.WaitOne();
- licznik++;
- sem.Release();
- stopWatch.Stop();
- }
- });
- watki.Add(watek);
- watek.Start(i);
- }
- foreach (var w in watki)
- {
- w.Join();
- }
- Console.WriteLine("Licznik: {0}",licznik);
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
- ts.Hours, ts.Minutes, ts.Seconds,
- ts.Milliseconds / 10);
- Console.WriteLine("Czas " + elapsedTime);
- Console.WriteLine("To już jest koniec, naciśnij ENTER...");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement