Github
Https://github.com/gongluck/Windows-Core-Program.git
11th chapter Windows thread pool. CPP: Defines the entry point for the application. #include "stdafx.h" #include "the 11th chapter of the Windows thread pool. h" VOID Ntapi SIMPLECB (_inout_ ptp_callback_instance INSTANCE, _i Nout_opt_ PVOID Context) {}void ntapi WORKCB (_inout_ ptp_callback_instance INSTANCE, _inout_opt_ PVOID Context, _inout_ ptp_work work) {}void Ntapi TIMERCB (_inout_ Ptp_callbac K_instance INSTANCE, _inout_opt_ PVOID Context, _inout_ ptp_timer TIMER) {static DWORD i = 0; i + = 1; Static Large_integer Li; Li. QuadPart = -10000000LL * i; Static FILETIME Duetime = {0}; Duetime.dwlowdatetime = li. LowPart; Duetime.dwhighdatetime = li. Highpart; Setthreadpooltimer (Timer, &duetime, 0, 0); Set Timer}void ntapi WAITCB (_inout_ ptp_callback_instance INSTANCE, _inout_opt_ PVOID Context, _inout_ ptp_wait WAIT, _in_ tp_wait_result waitresult) {switch (Waitresult) {Case Wait_object_0:break; Case Wait_timeout:break; Case Wait_abandoned:break; Default:break; }}void WINAPI IOCB (_inout_ ptp_callback_instance INSTANCE, _inout_opt_ PVOID Context, _inout _opt_ PVOID Overlapped, _in_ ULONG ioresult, _in_ ulong_ptr numberofbytestransferred, _inout_ ptp_io IO) {switch (ioresult) {case No_error:b Reak; Default:break; }}int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In _ int nCmdShow) {//Asynchronously call function BOOL Bres = Trysubmitthreadpoolcallback (SIMPLECB, nullptr, nullptr);//Add Work item to thread pool Queue Ptp_work pwork = Createthreadpoolwork (WORKCB, nullptr, nullptr); Create a work item submitthreadpoolwork (pwork); Submits a request to the thread pool Waitforthreadpoolworkcallbacks (pwork, false/* whether to first attempt to cancel the submitted work item */); CancelWork item or wait for completion closethreadpoolwork (pwork); Frees work item memory Pwork = nullptr; Call a function every once in a while ptp_timer Ptimer = Createthreadpooltimer (TIMERCB, nullptr, nullptr); Create timer Large_integer Li; Li. QuadPart = -1ll;//immediately begins FILETIME duetime = {0}; Duetime.dwlowdatetime = li. LowPart; Duetime.dwhighdatetime = li. Highpart; Setthreadpooltimer (Ptimer, &duetime, 1/* the time interval of the call again */, 0/* is used to add some randomness to the execution time of the callback function */); Set Timer waitforthreadpooltimercallbacks (Ptimer, FALSE); Debugging found that TIMERCB no chance to execute, nor block the main thread ah!? Bres = Isthreadpooltimerset (Ptimer); Check timer status//Call a function when the kernel object is triggered ptp_wait pwait = createthreadpoolwait (WAITCB, NULL, nullptr); Create thread pool Wait object HANDLE hevent = CreateEvent (nullptr, FALSE, TRUE, nullptr); Setthreadpoolwait (pwait, hevent, nullptr); Bind to Thread pool Waitforthreadpoolwaitcallbacks (pwait, FALSE); Call a function when the asynchronous IO request is complete HANDLE hfile = CreateFile (TEXT ("11th Windows thread pool. cpp"), Generic_read | Generic_write, File_share_read, nullptr, open_existing, file_flag_overlapped, NullptR); Ptp_io pio = Createthreadpoolio (hfile, IOCB, nullptr, nullptr); Create thread pool Io object char Buf[_max_path] = {0}; OVERLAPPED ol = {0}; Startthreadpoolio (PIO); Before each IO call, call Startthreadpoolio to enable the thread pool Io object bres = ReadFile (hfile, buf, _max_path, nullptr, &ol); Startthreadpoolio (PIO); Bres = ReadFile (hfile, buf, _max_path, nullptr, &ol); Waitforthreadpooliocallbacks (Pio, FALSE); Startthreadpoolio (PIO); Cancelthreadpoolio (PIO); Undo thread Pool Io Object bres = ReadFile (hfile, buf, _max_path, nullptr, &ol); System ("pause"); Setthreadpooltimer (Ptimer, nullptr, 0, 0); Cancel Timer closethreadpooltimer (Ptimer); Ptimer = nullptr; Setthreadpoolwait (pwait, nullptr, nullptr); Closethreadpoolwait (pwait); pwait = nullptr; CloseHandle (hevent); hevent = nullptr; Closethreadpoolio (PIO); Do not closethreadpoolio the PIO = nullptr after calling Cancelthreadpoolio; CloseHandle (hfile); hfile = nullptr; return 0;}
Windows core Programming: 11th Chapter Windows thread pool