Original Post
I often find myself writing code where I want to kick off a bunch of jobs, and then wait for them all to complete. I've come up with something like this:
int pendingJobCount = 0;
Event jobsComplete( true );
void AddJob()
{
int newCount = InterlockedIncrement( pendingJobCount );
if( newCount == 1 )
{
jobsComplete.Reset();
}
}
void FinishJob()
{
int newCount = InterlockedDecrement( pendingJobCount );
if( newCount == 0 )
{
jobsComplete.Signal();
}
}
AddJob();
AddJob();
AddJob();
...
jobsComplete.Wait();
Assert( pendingJobCount == 0 ); This seems to work, but I'm curious if this is a standard way of doing things, or if there's a better technique. Is there a more traditional synchronization primitive that will accomplish this?