日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程池之工作项,等待项,计时项 (存在疑问???)

發布時間:2025/3/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程池之工作项,等待项,计时项 (存在疑问???) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

線程池函數允許我們做:
1.以異步方式調用函數??//工作項
2.每隔一段時間調用一個函數?//計時項
3.在內核對象觸發時調用一個函數?//等待項
4.在異步I/O請求完成時調用一個函數 //I/O項

2.每隔一段時間調用一個函數
如果兩個:周期的每隔5-7秒執行一段代碼,周期的每隔6-8秒執行一段代碼。
不用線程池將有兩個線程執行,上下文切換不好,如用線程池,系統可能選擇6秒間隔只喚醒一個線程執行這兩段代碼。

3.在內核對象觸發時調用一個函數
同上類似,各個線程WaitForSingleObject各自的內核對象,如用線程池用一個線程WaitForMultipleObjects 最多MAXIMUM_WAIT_OBJECTS個內核對象
一旦回調函數被調用過,必須SetThreadpoolWait再注冊下,否則 等待項 是不活躍狀態不會再調用回調函數

?

#include <Windows.h>// C RunTime Header Files #include <string>PTP_WORK g_pWorkItem = NULL; volatile LONG g_nCurrentTask = 0;void NTAPI TaskHandler(PTP_CALLBACK_INSTANCE Instance, PVOID Context, PTP_WORK Work) {LONG currentTask = InterlockedIncrement(&g_nCurrentTask);wprintf(L"[%u] Task #%u is starting.\n", GetCurrentThreadId(), currentTask);// Simulate a lot of workSleep(currentTask * 1000);wprintf(L"[%u] Task #%u is done.\n", GetCurrentThreadId(), currentTask);InterlockedDecrement(&g_nCurrentTask); }void Work(void) {// Create the work item that will be used by all tasks//(1)create 線程池工作項g_pWorkItem = CreateThreadpoolWork(TaskHandler, NULL, NULL);if (g_pWorkItem == NULL) {wprintf(L"Impossible to create the work item for tasks.\n");return;}wprintf(L"----Start a new batch----\n");for(int i = 4; i > 0; --i){//(2)submit workSubmitThreadpoolWork(g_pWorkItem);}wprintf(L"4 tasks are submitted.\n");//Waits for outstanding work callbacks to complete and optionally cancels pending callbacks that have not yet started to execute.//(3)wait for work overWaitForThreadpoolWorkCallbacks(g_pWorkItem,FALSE);// Don't forget to delete the work item//(4)delete work itemCloseThreadpoolWork(g_pWorkItem); }PTP_TIMER g_timer = NULL; volatile LONG g_nSecLeft = 10; VOID CALLBACK TimeoutCallback(PTP_CALLBACK_INSTANCE pInst, PVOID pvContext, PTP_TIMER pTimer) {LONG current = InterlockedDecrement(&g_nSecLeft);if(current == 0){SetThreadpoolTimer(g_timer,NULL,0,0); //pftDueTime為NULL時,將停止調用定時器函數wprintf(L"time out----\n");}else{wprintf(L"[%u] call timer callback function #%u is left.\n", GetCurrentThreadId(), current);} }void Timer(void) {g_timer = CreateThreadpoolTimer(TimeoutCallback,NULL,NULL);// Start the timer in one second to trigger every 1 secondULARGE_INTEGER ulRelativeStartTime;ulRelativeStartTime.QuadPart = (LONGLONG) -1; // start nowFILETIME ftRelativeStartTime;ftRelativeStartTime.dwHighDateTime = ulRelativeStartTime.HighPart;ftRelativeStartTime.dwLowDateTime = ulRelativeStartTime.LowPart;SetThreadpoolTimer(g_timer, &ftRelativeStartTime, 1000, // Triggers every 1000 millisecondsINFINITE//0);//WaitForThreadpoolTimerCallbacks(g_timer,FALSE); //Why this will fail?getchar();CloseThreadpoolTimer(g_timer); }VOID NTAPI pttwaitcallback(_Inout_ PTP_CALLBACK_INSTANCE Instance,_Inout_opt_ PVOID Context,_Inout_ PTP_WAIT Wait,_In_ TP_WAIT_RESULT WaitResult) {wprintf(L"waitResult is WAIT_OBJECT_0 + %d.\n",WaitResult); }void WaitKernelObject() {PTP_WAIT pwait = CreateThreadpoolWait(pttwaitcallback,NULL,NULL);HANDLE hEvent1 = CreateEventW(NULL,FALSE,FALSE,L"event1");HANDLE hEvent2 = CreateEventW(NULL,FALSE,FALSE,L"event2");SetThreadpoolWait(pwait,hEvent1,NULL);SetEvent(hEvent1); //here will make pwait 不活躍狀態, 此狀態不會再去調回調函數。getchar();SetThreadpoolWait(pwait,hEvent2,NULL); //不活躍狀態后需要重新設置SetEvent(hEvent2);//WaitForThreadpoolWaitCallbacks(pwait,FALSE);getchar();CloseThreadpoolWait(pwait); } int wmain() {//Work();Timer();//WaitKernelObject();wprintf(L"it is over\n");getchar();return(0); }


?

總結

以上是生活随笔為你收集整理的线程池之工作项,等待项,计时项 (存在疑问???)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。