MSDN Magazine - October 2007 - (Page 62) Figure 7 I/O Completion Object APIs PTP_IO WINAPI CreateThreadpoolIo(HANDLE fl, PTP_WIN32_IO_CALLBACK pfnio, PVOID pv, PTP_CALLBACK_ENVIRON pcbe); VOID WINAPI StartThreadpoolIo(PTP_IO pio); VOID CALLBACK IoCompletionCallback(PTP_CALLBACK_INSTANCE Instance, PVOID Context, PVOID Overlapped, ULONG IoResult, ULONG_PTR NumberOfBytesTransferred, PTP_IO Io); VOID WINAPI WaitForThreadpoolIoCallbacks(PTP_IO pio, BOOL fCancelPendingCallbacks); VOID WINAPI CancelThreadpoolIo(PTP_IO pio); VOID WINAPI WaitForThreadpoolIoCallbacks(PTP_IO pio, BOOL fCancelPendingCallbacks); VOID WINAPI CloseThreadpoolIo(PTP_IO pio); pftDueTime parameter is NULL, it will stop queuing callbacks to the TimerCallback function but callbacks that have already been queued will be executed. Thus you can cancel a timer without having to close the timer object so that it can be reused. Once the timer object comes due, a request to invoke the callback will be queued to the thread pool. A worker thread will pick up the request and invoke the TimerCallback function supplied in the call to SetThreadpoolTimer. The parameters to the callback function are the same as those described for a work object callback function, with the exception that the third parameter is a PTP_TIMER instead of a PTP_WORK. Of the remaining timer API functions in Figure 5, the IsThreadpoolTimerSet function, as its name implies, returns TRUE if the timer has been set and FALSE otherwise. The remaining two, WaitForThreadpoolTimerCallbacks and CloseThreadpoolTimer behave exactly as their work object counterparts do. cause memory corruption. You must call a related function, CancelThreadpoolIo, when the call to initiate an asynchronous I/O operation returns with a failure other than ERROR_IO_PENDING. Accidently omitting the call to CancelThreadpoolIo will cause the thread pool to leak memory. Once an I/O completion occurs, a worker thread will call the IoCompletionCallback associated with the I/O completion object. The signature for the function follows the same convention as all the other callbacks: the first two parameters to the function are a pointer to the callback instance and the context pointer supplied to the create function. The third parameter, Overlapped, is the pointer to the overlapped structure supplied when initiating the asynchronous I/O operation. The fourth is IoResult, which contains the result of the operation. IoResult will contain NO_ ERROR if the operation completed successfully; otherwise it will contain a system error code (see msdn2.microsoft.com/ms681381). The fifth parameter, NumberOfBytesTransferred, contains the number of bytes transferred during the I/O operation while the sixth parameter is a pointer to the I/O completion object itself. Once again, the remaining functions in Figure 7,WaitForThreadpoolIoCallbacks and CloseThreadpoolIo, behave just like their work object counterparts. Simplifying Cleanup with Cleanup Groups Now let’s see how cleanup groups can help simplify the process of cleaning up the thread pool callback objects an application creates. By associating a cleanup group with a thread pool callback environment, all thread pool callback objects created using that callback environment will be tracked by the cleanup group. Once your application is finished using callback objects, all it has to do to ensure that each one is closed is to make one function call instead of one close call for each callback object. In fact, if a callback object is associated with a cleanup group, the close API for it shouldn’t be called. The first step in using a cleanup group is to create a cleanup group object using the CreateThreadpoolCleanupGroup function, which is shown in Figure 8. The next step is to associate the cleanup group with the callback environment it is to be used with. The SetThreadpoolCallbackCleanupGroup function from Figure 3 sets up this association. The first parameter to the function is a pointer to the callback environment. The second parameter is the PTP_CLEANUP_ GROUP, and the third parameter is a pointer to a callback function that will be invoked when the CloseThreadpoolCleanupGroupMembers function is called. Once an application has finished using the thread pool callback Figure 8 Cleanup Group APIs PTP_CLEANUP_GROUP WINAPI CreateThreadpoolCleanupGroup(void); VOID WINAPI CloseThreadpoolCleanupGroupMembers(PTP_CLEANUP_GROUP ptpcg, BOOL fCancelPendingCallbacks, PVOID pvCleanupContext); VOID CALLBACK CleanupGroupCancelCallback(PVOID ObjectContext, PVOID CleanupContext); VOID WINAPI CloseThreadpoolCleanupGroup(PTP_CLEANUP_GROUP ptpcg); I/O Completion Objects The final type of callback object supported by the thread pool is the I/O completion object. Figure 7 lists all the APIs relating to I/O completion objects. An I/O completion object is used to bind a file handle to the thread pool so that asynchronous I/O completion notifications are queued to the thread pool for processing by worker threads. The parameters for the CreateThreadpoolIo function follow a similar pattern to the other create functions with the addition of the HANDLE parameter, which must be opened for overlapped I/O completion. As shown in Figure 7, the first parameter to CreateThreadpoolIo is the HANDLE to receive completion notifications, while parameters two, three, and four represent respectively the pointer to the callback function to be invoked, an optional application-specific context, and a pointer to the callback environment. CreateThreadpoolIo returns a non-NULL pointer if successful and a NULL pointer otherwise. In order to cause I/O completion notifications to be processed by the thread pool, the StartThreadpoolIo function must be called prior to issuing each asynchronous I/O request operation on the handle. Forgetting to do this has serious consequences because the thread pool will ignore the I/O completion when it occurs and 62 msdnmagazine Thread Pools http://msdn2.microsoft.com/ms681381
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.