MSDN Magazine - October 2007 - (Page 60) Figure 5 Wait Object APIs PTP_WAIT WINAPI CreateThreadpoolWait(PTP_WAIT_CALLBACK pfnwa, PVOID pv, PTP_CALLBACK_ENVIRON pcbe); VOID WINAPI SetThreadpoolWait(PTP_WAIT pwa, HANDLE h, PFILETIME pftTimeout); VOID CALLBACK WaitCallback(PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WAIT Wait, TP_WAIT_RESULT WaitResult); VOID WINAPI WaitForThreadpoolWaitCallbacks(PTP_WAIT pwa, BOOL fCancelPendingCallbacks); VOID WINAPI CloseThreadpoolWait(PTP_WAIT pwa); pwa, is a pointer to the wait object instance being set. The second parameter is the HANDLE to the kernel object to wait on. The final parameter, pftTimeout, is a pointer to a FILETIME structure that indicates the amount of time the thread pool should wait for the kernel object to be signaled. The amount of time to wait can be expressed in absolute or relative terms and is specified in 100-nanosecond units. Passing a positive value indicates that the timeout is an absolute time since 1/1/1600. Passing a negative value indicates a time relative to the current time at the call of the function. Passing a 0 indicates that the wait times out immediately. Finally, passing NULL indicates an infinite wait that never times out. Once the kernel object becomes signaled or the wait times out, the thread pool will invoke the wait object’s WaitCallback function. The first two parameters are exactly the same as in the WorkCallback function described earlier. The Wait parameter indicates which wait object the callback is being invoked for and the WaitResult is used to indicate the reason for the invocation. WaitResult will be WAIT_ABANDONED_0, WAIT_OBJECT_0, or WAIT_TIMEOUT. If WaitResult is set to WAIT_OBJECT_0, it means that the kernel object became signaled and the wait was satisfied. If WaitResult is set to WAIT_TIMEOUT, it means the wait is unsatisfied and the timeout interval specified in the call to SetThreadpoolWait has elapsed. A result of WAIT_ABANDONED_0 indicates that the specified object is a mutex and that it was not released by the thread that owned the mutex object before it terminated. However, using a mutex with a wait object should be avoided because the worker thread that invokes the WaitCallback function is not the thread that performed the wait on the mutex. The thread pool uses a different type of thread, called a waiter thread, to actually Figure 6 Timer Object APIs PTP_TIMER WINAPI CreateThreadpoolTimer(PTP_TIMER_CALLBACK pfnti, PVOID pv, PTP_CALLBACK_ENVIRON pcbe); VOID WINAPI SetThreadpoolTimer(PTP_TIMER pti, PFILETIME pftDueTime, DWORD msPeriod, DWORD msWindowLength); VOID CALLBACK TimerCallback(PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_TIMER Timer); VOID WINAPI WaitForThreadpoolTimerCallbacks(PTP_TIMER pti, BOOL fCancelPendingCallbacks); BOOL WINAPI IsThreadpoolTimerSet(PTP_TIMER pti); VOID WINAPI CloseThreadpoolTimer(PTP_TIMER pti); perform the wait on the kernel object. It is the waiter thread that actually owns the mutex. Waiter threads are not exposed to applications, so there is no way to release the mutex once the waiter thread acquires ownership of it. After the WaitCallback function has been called for a wait object, SetThreadpoolWait must be called again in order reuse the wait object and have the thread pool wait again for a kernel object to become signaled. Note that when calling SetThreadpoolWait again to reuse a wait object, you have the option to once again specify any kernel object handle to wait on. You don’t have to use the handle that was specified in the first call to SetThreadpoolWait if you want to wait on a different kernel object. Finally, the remaining wait object APIs, WaitForThreadpoolWaitCallbacks and CloseThreadpoolWait, behave in exactly the same manner as their work object counterparts. Timer Objects Timer objects are used to invoke a callback function when the timer object reaches its due time. They are created using the CreateThreadpoolTimer function, which is shown in Figure 6 along with the other timer object-related APIs. Once again the parameters follow the same pattern as the CreateThreadpoolWork function (in Figure 4), with the exception that the pointer to the callback function supplied in the first parameter must match the signature of the TimerCallback function, as shown in Figure 6. After creating a timer object, the next step is to set it using the SetThreadpoolTimer function. The pftDueTime parameter is used to set the time when the timer should initially come due. This time is expressed in the same way as the time out used in the SetThreadpoolWait function described above. As its name suggests, the msPeriod parameter sets up a timer, expressed in milliseconds, that fires periodically. Therefore, once the initial time interval expressed by pftDueTime has expired and caused a callback to be queued to the thread pool, each subsequent time interval expiration defined by msPeriod will cause another callback to be queued. The msWindowLength parameter specifies a time window, expressed in milliseconds, during which the thread pool may delay expiration of the timer. This parameter promotes efficiency when you’re using a large number of timers and the expiration time doesn’t have to be exact. The window is a fudge factor of sorts that allows the system to coalesce all the timer expirations that fall into the window together so that they can be batched. This is more efficient than waking up a thread, expiring one timer, sleeping, waking up a thread, expiring another timer, sleeping, and so forth. The delay will occur only if there aren’t any timers in the window that absolutely must be expired. To better understand how msWindowLength might be used, consider a server app that has a large number of incoming client connections and that in order to reduce resource usage, connections that haven’t been active for five minutes should be closed. In this case, specifying a non-zero window length for the inactivity timers, which may result in keeping a connection around slightly past its due time, may be acceptable. SetThreadpoolTimer can also be used to set a new due time, period, and window length for a timer that was previously set. If the 60 msdnmagazine Thread Pools
Table of Contents Feed for the Digital Edition of MSDN Magazine - October 2007 Cover Contents Toolbox CLR Inside Out Basic Instincts Data Points Cutting Edge Pooled Threads WPF Threads Parallel Linq Parallel Performance Mobile Apps Test Run Foundations Windows with C++ Netting C++ .NET Matters { End Bracket } Net Nuptials MSDN Magazine - October 2007 MSDN Magazine - October 2007 - Contents (Page Cover1) MSDN Magazine - October 2007 - Contents (Page Cover2) MSDN Magazine - October 2007 - Contents (Page 1) MSDN Magazine - October 2007 - Contents (Page 2) MSDN Magazine - October 2007 - Contents (Page 3) MSDN Magazine - October 2007 - Contents (Page 4) MSDN Magazine - October 2007 - Contents (Page 5) MSDN Magazine - October 2007 - Contents (Page 6) MSDN Magazine - October 2007 - Contents (Page 7) MSDN Magazine - October 2007 - Contents (Page 8) MSDN Magazine - October 2007 - Contents (Page 9) MSDN Magazine - October 2007 - Contents (Page 10) MSDN Magazine - October 2007 - Toolbox (Page 11) MSDN Magazine - October 2007 - Toolbox (Page 12) MSDN Magazine - October 2007 - Toolbox (Page 13) MSDN Magazine - October 2007 - Toolbox (Page 14) MSDN Magazine - October 2007 - Toolbox (Page 15) MSDN Magazine - October 2007 - Toolbox (Page 16) MSDN Magazine - October 2007 - CLR Inside Out (Page 17) MSDN Magazine - October 2007 - CLR Inside Out (Page 18) MSDN Magazine - October 2007 - CLR Inside Out (Page 19) MSDN Magazine - October 2007 - CLR Inside Out (Page 20) MSDN Magazine - October 2007 - CLR Inside Out (Page 21) MSDN Magazine - October 2007 - CLR Inside Out (Page 22) MSDN Magazine - October 2007 - CLR Inside Out (Page 23) MSDN Magazine - October 2007 - CLR Inside Out (Page 24) MSDN Magazine - October 2007 - CLR Inside Out (Page 25) MSDN Magazine - October 2007 - CLR Inside Out (Page 26) MSDN Magazine - October 2007 - CLR Inside Out (Page 27) MSDN Magazine - October 2007 - CLR Inside Out (Page 28) MSDN Magazine - October 2007 - CLR Inside Out (Page 29) MSDN Magazine - October 2007 - CLR Inside Out (Page 30) MSDN Magazine - October 2007 - Basic Instincts (Page 31) MSDN Magazine - October 2007 - Basic Instincts (Page 32) MSDN Magazine - October 2007 - Basic Instincts (Page 33) MSDN Magazine - October 2007 - Basic Instincts (Page 34) MSDN Magazine - October 2007 - Basic Instincts (Page 35) MSDN Magazine - October 2007 - Basic Instincts (Page 36) MSDN Magazine - October 2007 - Data Points (Page 37) MSDN Magazine - October 2007 - Data Points (Page 38) MSDN Magazine - October 2007 - Data Points (Page 39) MSDN Magazine - October 2007 - Data Points (Page 40) MSDN Magazine - October 2007 - Data Points (Page 41) MSDN Magazine - October 2007 - Data Points (Page 42) MSDN Magazine - October 2007 - Cutting Edge (Page 43) MSDN Magazine - October 2007 - Cutting Edge (Page 44) MSDN Magazine - October 2007 - Cutting Edge (Page 45) MSDN Magazine - October 2007 - Cutting Edge (Page 46) MSDN Magazine - October 2007 - Cutting Edge (Page 47) MSDN Magazine - October 2007 - Cutting Edge (Page 48) MSDN Magazine - October 2007 - Cutting Edge (Page 49) MSDN Magazine - October 2007 - Cutting Edge (Page 50) MSDN Magazine - October 2007 - Cutting Edge (Page 51) MSDN Magazine - October 2007 - Cutting Edge (Page 52) MSDN Magazine - October 2007 - Cutting Edge (Page 53) MSDN Magazine - October 2007 - Pooled Threads (Page 54) MSDN Magazine - October 2007 - Pooled Threads (Page 55) MSDN Magazine - October 2007 - Pooled Threads (Page 56) MSDN Magazine - October 2007 - Pooled Threads (Page 57) MSDN Magazine - October 2007 - Pooled Threads (Page 58) MSDN Magazine - October 2007 - Pooled Threads (Page 59) MSDN Magazine - October 2007 - Pooled Threads (Page 60) MSDN Magazine - October 2007 - Pooled Threads (Page 61) MSDN Magazine - October 2007 - Pooled Threads (Page 62) MSDN Magazine - October 2007 - Pooled Threads (Page 63) MSDN Magazine - October 2007 - Pooled Threads (Page 64) MSDN Magazine - October 2007 - Pooled Threads (Page 65) MSDN Magazine - October 2007 - WPF Threads (Page 66) MSDN Magazine - October 2007 - WPF Threads (Page 67) MSDN Magazine - October 2007 - WPF Threads (Page 68) MSDN Magazine - October 2007 - WPF Threads (Page 69) MSDN Magazine - October 2007 - Parallel Linq (Page 70) MSDN Magazine - October 2007 - Parallel Linq (Page 71) MSDN Magazine - October 2007 - Parallel Linq (Page 72) MSDN Magazine - October 2007 - Parallel Linq (Page 73) MSDN Magazine - October 2007 - Parallel Linq (Page 74) MSDN Magazine - October 2007 - Parallel Linq (Page 75) MSDN Magazine - October 2007 - Parallel Linq (Page 76) MSDN Magazine - October 2007 - Parallel Linq (Page 77) MSDN Magazine - October 2007 - Parallel Linq (Page 78) MSDN Magazine - October 2007 - Parallel Performance (Page 79) MSDN Magazine - October 2007 - Parallel Performance (Page 80) MSDN Magazine - October 2007 - Parallel Performance (Page 81) MSDN Magazine - October 2007 - Parallel Performance (Page 82) MSDN Magazine - October 2007 - Parallel Performance (Page 83) MSDN Magazine - October 2007 - Parallel Performance (Page 84) MSDN Magazine - October 2007 - Parallel Performance (Page 85) MSDN Magazine - October 2007 - Parallel Performance (Page 86) MSDN Magazine - October 2007 - Parallel Performance (Page 87) MSDN Magazine - October 2007 - Parallel Performance (Page 88) MSDN Magazine - October 2007 - Parallel Performance (Page 89) MSDN Magazine - October 2007 - Parallel Performance (Page 90) MSDN Magazine - October 2007 - Mobile Apps (Page 91) MSDN Magazine - October 2007 - Mobile Apps (Page 92) MSDN Magazine - October 2007 - Mobile Apps (Page 93) MSDN Magazine - October 2007 - Mobile Apps (Page 94) MSDN Magazine - October 2007 - Mobile Apps (Page 95) MSDN Magazine - October 2007 - Mobile Apps (Page 96) MSDN Magazine - October 2007 - Mobile Apps (Page 97) MSDN Magazine - October 2007 - Mobile Apps (Page 98) MSDN Magazine - October 2007 - Mobile Apps (Page 99) MSDN Magazine - October 2007 - Mobile Apps (Page 100) MSDN Magazine - October 2007 - Test Run (Page 101) MSDN Magazine - October 2007 - Test Run (Page 102) MSDN Magazine - October 2007 - Test Run (Page 103) MSDN Magazine - October 2007 - Test Run (Page 104) MSDN Magazine - October 2007 - Test Run (Page 105) MSDN Magazine - October 2007 - Test Run (Page 106) MSDN Magazine - October 2007 - Test Run (Page 107) MSDN Magazine - October 2007 - Test Run (Page 108) MSDN Magazine - October 2007 - Test Run (Page 109) MSDN Magazine - October 2007 - Test Run (Page 110) MSDN Magazine - October 2007 - Test Run (Page 111) MSDN Magazine - October 2007 - Test Run (Page 112) MSDN Magazine - October 2007 - Test Run (Page 113) MSDN Magazine - October 2007 - Test Run (Page 114) MSDN Magazine - October 2007 - Foundations (Page 115) MSDN Magazine - October 2007 - Foundations (Page 116) MSDN Magazine - October 2007 - Foundations (Page 117) MSDN Magazine - October 2007 - Foundations (Page 118) MSDN Magazine - October 2007 - Foundations (Page 119) MSDN Magazine - October 2007 - Foundations (Page 120) MSDN Magazine - October 2007 - Foundations (Page 121) MSDN Magazine - October 2007 - Foundations (Page 122) MSDN Magazine - October 2007 - Foundations (Page 123) MSDN Magazine - October 2007 - Foundations (Page 124) MSDN Magazine - October 2007 - Windows with C++ (Page 125) MSDN Magazine - October 2007 - Windows with C++ (Page 126) MSDN Magazine - October 2007 - Windows with C++ (Page 127) MSDN Magazine - October 2007 - Windows with C++ (Page 128) MSDN Magazine - October 2007 - Windows with C++ (Page 129) MSDN Magazine - October 2007 - Windows with C++ (Page 130) MSDN Magazine - October 2007 - Windows with C++ (Page 131) MSDN Magazine - October 2007 - Windows with C++ (Page 132) MSDN Magazine - October 2007 - Netting C++ (Page 133) MSDN Magazine - October 2007 - Netting C++ (Page 134) MSDN Magazine - October 2007 - Netting C++ (Page 135) MSDN Magazine - October 2007 - Netting C++ (Page 136) MSDN Magazine - October 2007 - .NET Matters (Page 137) MSDN Magazine - October 2007 - .NET Matters (Page 138) MSDN Magazine - October 2007 - .NET Matters (Page 139) MSDN Magazine - October 2007 - .NET Matters (Page 140) MSDN Magazine - October 2007 - .NET Matters (Page 141) MSDN Magazine - October 2007 - .NET Matters (Page 142) MSDN Magazine - October 2007 - .NET Matters (Page 143) MSDN Magazine - October 2007 - Net Nuptials (Page 144) MSDN Magazine - October 2007 - Net Nuptials (Page Cover3) MSDN Magazine - October 2007 - Net Nuptials (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.