|
イベントで待ち合わせてみた。
using System;
using System.Threading;
using System.Threading.Tasks;
class Program {
static EventWaitHandle ewh;
public static void Main() {
const int N = 10;
ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
Parallel.For(
0, N+1,
i =>
{
if ( i != N ) {
Console.WriteLine("Thread " + i + " started (wait)");
// 合図を待つ
ewh.WaitOne();
Console.WriteLine("Thread " + i + " started (resume)");
while (true)
{
Thread.Sleep(10);
}
} else {
// 最後の一つが号砲を鳴らす
Console.WriteLine("--- GO! ---");
ewh.Set();
}
});
Console.ReadKey();
}
}
|