11th Chapter Windows thread pool
11.1 traditional Windows thread pool and API
11.1.1 traditional thread pool objects and their corresponding APIs
Thread Pool object |
API |
Normal task thread pool |
QueueUserWorkItem |
Timer thread pool |
Createtimerqueue (create thread pool) |
Createtimerqueuetimer (create timer) |
Changetimerque Uetimer |
Deletetimerqueuetimer |
Detetimerqueueex |
Synchronization object wait thread pool |
Regist Erwaitforsingleobject |
Unregisterwaitex |
Complete port thread pool |
Bindiocompletioncallback |
11.1.2 Common task thread pool
(1) QueueUserWorkItem function
ULONG Flags
parameters |
description |
Lpthread_start_routine Pfncallback |
Work item, which is a callback function that is queued to the thread pool (similar to a thread function), the prototype is declared DWORD WINAPI ThreadProc (LPVOID lpparameter); |
PVOID Context |
Additional data to pass to the thread function |
is used to indicate under what conditions a thread in a thread pool calls this callback function ①wt_ Executedefault: Normal threads do not operate in a warning state. ②wt_executeiniothread: Runs the thread callback function in an IO-warning state. ③wt_executeinpersistentthread: The thread has been running without terminating. ④wt_executelongfunction: Performs a long-running task (which causes the system to consider whether to create new threads in the thread pool). ⑤wt_transfer_impersonation: Run the thread with the current access token and callback function |
Note: The CreateThread function is consistent with the prototype of the threading function required by the QueueUserWorkItem function. Therefore, it is convenient to create a thread function as a thread pool callback function for threads or pool. |
"QueueUserWorkItem Sample Program"
#include <windows.h>#include<locale.h>#include<tchar.h>#include<strsafe.h>#defineBeginthread (Fun,param) CreateThread (null,0, (lpthread_start_routine) fun,param,0,null);DWORD WINAPI ThreadProc (LPVOID lpparameter);int_tmain () {_tsetlocale (Lc_all, _t ("CHS")); intIwaitlen =0; Do{_tprintf (_t ("Please enter a waiting time constant, in seconds (enter 0 to exit):")); _tscanf_s (_t ("%i"), &Iwaitlen); if(iwaitlen>0){ //The following code demonstrates that using CreateThread and QueueUserWorkItem, the actual effect//is the same, of course, if there are not many threads, be sure to use QueueUserWorkItem if there are many threadsQueueUserWorkItem (ThreadProc, (PVOID) Iwaitlen, wt_executelongfunction); //display the effect of using CreateThread to create multiple threads//Beginthread (ThreadProc, (LPVOID) iwaitlen); } } while(Iwaitlen); return 0;}//The function can be started by a thread of createthread, or it can be started using a thread in the QueueUserWorkItem thread poolDWORD WINAPI ThreadProc (lpvoid lpparameter) {intIwaitlen = (int) Lpparameter; _tprintf (_t ("\ nthe thread [id:0x%x] will wait for%u seconds ..."), GetCurrentThreadID (), Iwaitlen); Sleep (Iwaitlen* +); _tprintf (_t ("\ nthe thread [id:0x%x] will wait for the end! \ n"), GetCurrentThreadID (), Iwaitlen); return 0;}
11th Chapter Windows thread Pool (1) _ Traditional Windows thread pool