Wednesday, November 05, 2008


Did you ever have a case where you have a big dataset which can be processed in parallel, so long as all threads finish Step 1 before any thread starts Step 2? Thing is, there's no built in class to handle this case.

AutoResetEvent won't do it because it because it only signals *one* of the waiting threads, not *all* of them. What you need is a manual reset event and some complex handling.

I give you BarrierHandle:

using System;
using System.Threading;

class SpectralNorm
public class BarrierHandle : System.Threading.WaitHandle
int current;
int threads;
ManualResetEvent handle = new ManualResetEvent (false);

public BarrierHandle (int threads)
this.current = threads;
this.threads = threads;

public override bool WaitOne()
ManualResetEvent h = handle;
if (Interlocked.Decrement (ref current) > 0) {
h.WaitOne ();
else {
handle = new ManualResetEvent (false);
Interlocked.Exchange (ref current, threads);
h.Set ();
h.Close ();

return true;

public static void Main(String[] args)
int threadCount = 5;//Environment.ProcessorCount;

// Lets assume that there's 20 units of data for each thread
int[] dataset = new int [threadCount * 20];

// This is the handle we use to ensure all threads complete the current step
// before continuing to the next step
BarrierHandle barrier = new BarrierHandle(threadCount);

// Fire up the threads
for (int i = 0; i < threadCount; i++) {
ThreadPool.QueueUserWorkItem (delegate {
int jjj = i;
try {
Step1 (dataset, jjj * 20, 20);
barrier.WaitOne ();
Step2 (dataset, jjj * 20, 20);
barrier.WaitOne ();
Step3 (dataset, jjj * 20, 20);
} catch (Exception ex) {
Console.WriteLine (ex);

System.Threading.Thread.Sleep (3000);

private static void Step1 (int[] array, int offset, int count)
Console.WriteLine ("Step1: {0} - {1}", offset, offset + count);
Thread.Sleep (500);
private static void Step2 (int[] array, int offset, int count)
Console.WriteLine ("Step2: {0} - {1}", offset, offset + count);
Thread.Sleep (500);
private static void Step3 (int[] array, int offset, int count)
Console.WriteLine ("Step3: {0} - {1}", offset, offset + count);
Thread.Sleep (500);


Garuma said...

There is CountdownEvent class in ParallelFx which can be used for that.

smallawei said...


酒店上班請找艾葳 said...

艾葳酒店經紀是合法的公司、我們是不會跟水水簽任何的合約 ( 請放心 ),我們是不會強押水水辛苦工作的薪水,我們絕對不會對任何人公開水水的資料、工作環境高雅時尚,無業績壓力,無脫秀無喝酒壓力,高層次會員制客源,工作輕鬆。

Bruce said...

Today,we are proud to announce the launch of the new wedding support service sell ffxi gil,packed with features sure to sell ffxi gils delight adventurers across Vana'diel looking to exchange eternal vows with their beloved!Responding to player demands for greater customization,the new service will grant brides and grooms freedom in choosing location,timing,dialogue,and sell Final Fantasy XI Gil more for their ceremony,allowing them to create a truly memorable event all their own.Information on all the features,including in-game sell ffxi gil item vendors and wedding certificates,can be found on the new wedding support site,so head on over sell ffxi gils and get started planning the wedding of your dreams sell Final Fantasy XIGil!

gaohui said...

Have you noticed ed hardy Clothing that she is spending time with ed hardy sale one person in particular ed hardy and they seemed to come from ed hardy UK nowhere. When you ask how she ed hardy cheap knows them she becomes aloof and ed hardy Clothes disinterested. Is there someone's house ed hardy store she seems to be always going to? This could spell something is wrong with the christian audigier sale relationship. Is she taking trips, possibly day ed hardy dresses trips or small vacations without you? If ed hardy Polos she was doing this before you even ed hardy sandals got married or dated, then it may be okay, but if it is a recent ed hardy Jackets development then you may have problems.

Hit Counter