I have some work items pooled using the legacy QueueUserWorkItem function (I need to support OSs earlier than vista, so
for( <loop thru some items >)
{
QueueUserWorkItem( )
}
I need to wait on these before proceeding to the next step. I've seen several similar answers...but they are in .NET. I'm thinking of using a storing an Event for each item and waiting on them(gasp!), but are there other better, lightweight ways? (no kernel locks)
Clarification: I know of using Events. I'm interested in solutions that don't require a kernel-level lock.
AFAIK the only ways you can do this is to have a counter that is InterlockIncrement'ed as each task finishes.
You can then either do a
while( counter < total )
Sleep( 0 );
of the task can signal an event (or other sync object) and you can do the following
while( count < total )
WaitForSingleObject( hEvent, INFINITE );
The second method will mean that the main thread uses less processing time.
Edit: TBH the only way to avoid a kernel lock is to spin lock and that will mean you'll have one core wasting time that could otherwise be used to process your work queue (or indeed anything else). If you REALLY must avoid a kernel lock then spin lock with a Sleep( 0 ). However I'd definitely recommend just using a kernel lock, get the extra CPU time back for doing worthwhile processing and stop worrying about a 'non' problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With