Created
July 7, 2016 06:49
-
-
Save bohdantrotsenko/7be275f59fe0d97b8ec2b753264a58b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Threading; | |
class Program | |
{ | |
ManualResetEvent exit = new ManualResetEvent(false); | |
Thread[] ths; | |
int counter = 0; | |
void DummyWorker() | |
{ | |
exit.WaitOne(); // очікування події, що почали всі "одночасно", а до того були в пам'яті | |
Interlocked.Increment(ref counter); | |
} | |
int i; | |
void Run(int N) | |
{ | |
ths = new Thread[N]; | |
try | |
{ | |
var sw = Stopwatch.StartNew(); | |
Console.WriteLine("Створюю потоки..."); | |
for (i = 0; i < N; i++) | |
ths[i] = new Thread(DummyWorker) { IsBackground = true }; | |
Console.WriteLine("Запускаю потоки... ({0})", sw.Elapsed); | |
for (i = 0; i < N; i++) | |
ths[i].Start(); | |
Console.WriteLine("Очікую завершення... ({0})", sw.Elapsed); | |
exit.Set(); | |
for (i = 0; i < N; i++) | |
{ | |
ths[i].Join(); // очіємо завершення і-го потоку | |
ths[i] = null; | |
} | |
Console.WriteLine("Все. ({0})", sw.Elapsed); | |
Console.WriteLine("Лічильник = {0}", counter); | |
} | |
catch (OutOfMemoryException) | |
{ | |
Console.WriteLine("закінчилась пам'ять при i = {0}", i); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
int N; | |
if (!(args.Length > 0 && int.TryParse(args[0], out N))) | |
{ | |
Console.Write("N: "); | |
N = int.Parse(Console.ReadLine().Trim()); | |
} | |
new Program().Run(N); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment