views:

277

answers:

2

I recently discovered ATL's CThreadPool class and was very happy with this find. It's a neat little class that will take care of the syncronization semantics of having multiple worker threads to process some queue of taks to do. The tasks are fed to the CThreadPool object, by some extrnal process.

While being very neat an clean, there doesn't seem to be a way find out whether all tasks have catually been completed. What's the best way to do that?

For example, say I need to do 10 heavy computational tasks and then move on to do something else. I can't move on until I know the tasks have completed.

So I create a CThreadPool with 10 threads, put the tasks on the queue and off they go to the threads. How would I know when the tasks are completed?

Boaz

A: 

I think that you'll need to have your worker object's Execute() method use some form of IPC that the 'main' thread can monitor. Something like have the Execute() method call ReleaseSemaphore() on a semaphore that the main thread can wait on and count how many times the wait completes. Or the thread pool workers can post a message indicating they are done with a work item to a message queue the main thread gets messages from.

Michael Burr
+1  A: 

The cleanest way of waiting for a completed task is by creating an event, then waiting for the worker thread to finish by calling WaitForSingleObject.

Alan