Download source code from MSDN Code Gallery.
Till now we have seen how to use the default process-wide threadpool to submit work items, how to create your own private threadpool and how to create timer objects to execute callbacks when the timer fires. In this post (final one in the threadpool series), we will see how to create waiter objects so that you can run your callback code when an event fires.
Both the windowsthreadpool::SimpleThreadPool and windowsthreadpool::PrivateThreadPool class support the following member functions to support events; RegisterEvent, ReRegisterEvent and DestroyEvent. To wait on an event, you need to call RegisterEvent; once that event fires, you need to call ReRegisterEvent to wait on it again. DestroyEvent closes the wait object in the threadpool so that you no longer wait on that event.
In this example, we create an event and register interest in the event by queuing a work item to the process-wide threadpool along with a pointer to our callback (HelloWorld). When the event is set, the callback “HelloWorld” executes. A different callback (HelloWorld2) is registered with the private threadpool for the same event. This callback will run twice.
The RegisterEvent method takes in a function pointer to the callback function, an event handle and a timeout value. If the timeout is -1, then it waits indefinitely for the event. The RegisterEvent method returns an opaque handle which is passed as input to the DestroyEvent and ReRegisterEvent methods. Once a raised event is handled in the callback, you need to re-register interest in the event for the threadpool to call your callback again.
The majority of the work for these methods is happening in the internal class WaitCallBack. When you register an event, it first creates TP_WAIT object by calling CreateThreadpoolWait using the same environment block for all work items queued via the same threadpool. It then passes this wait object along with the event handle and a timeout value to SetThreadpoolWait. If a user wants to wait indefinitely for the event to fire, then you need to pass NULL as the timeout value to SetThreadpoolWait.
The ReRegisterEvent method simply calls the SetThreadpoolWait as shown above.
The DestroyEvent method indicates that you are no longer interested in this event. Remember back in the first post I mentioned that if you call CloseThreadpoolCleanupGroupMembers, it allows you to free all work objects, wait objects and timers associated with the cleanup group in one call. So that’s why in the DestroyEvent method, we check if WaitForAll has already been called (which would imply the wait object is already freed) and if not then call the internal DestroyEvent method.
And that’s it! Over the course of this series on threadpool we have learnt how to queue work items to the default process-wide threadpool, create your own private threadpool, queue work items to it, create a background threadpool which runs low priority work items, create timer objects and wait on events to run code when the events are signaled.
The only functionality that’s not covered in this series is using the threadpool to perform asynchronous IO. Maybe I will cover that in a later series. In the meantime, if you have any questions on the win32 threadpool, please leave a comment and I will try to get it answered.