使用線程池功能

此示例創建自定義線程池,創建工作項和線程池計時器,并將它們與清理組關聯。該池由一個持久性線程組成。它演示了以下線程池函數的使用:

  • CloseThreadpool
  • CloseThreadpoolCleanupGroup
  • CloseThreadpoolCleanupGroupMembers
  • CloseThreadpoolWait
  • CreateThreadpool
  • CreateThreadpoolCleanupGroup
  • CreateThreadpoolTimer
  • CreateThreadpoolWait
  • CreateThreadpoolWork
  • InitializeThreadpoolEnvironment
  • SetThreadpoolCallbackCleanupGroup
  • SetThreadpoolCallbackPool
  • SetThreadpoolThreadMaximum
  • SetThreadpoolThreadMinimum
  • SetThreadpoolTimer
  • SetThreadpoolWait
  • SubmitThreadpoolWork
  • WaitForThreadpoolWaitCallbacks
#include <windows.h>
#include <tchar.h>
#include <stdio.h>//
// Thread pool wait callback function template
//
VOID
CALLBACK
MyWaitCallback(PTP_CALLBACK_INSTANCE Instance,PVOID                 Parameter,PTP_WAIT              Wait,TP_WAIT_RESULT        WaitResult)
{// Instance, Parameter, Wait, and WaitResult not used in this example.UNREFERENCED_PARAMETER(Instance);UNREFERENCED_PARAMETER(Parameter);UNREFERENCED_PARAMETER(Wait);UNREFERENCED_PARAMETER(WaitResult);//// Do something when the wait is over.//_tprintf(_T("MyWaitCallback: wait is over.\n"));
}//
// Thread pool timer callback function template
//
VOID
CALLBACK
MyTimerCallback(PTP_CALLBACK_INSTANCE Instance,PVOID                 Parameter,PTP_TIMER             Timer)
{// Instance, Parameter, and Timer not used in this example.UNREFERENCED_PARAMETER(Instance);UNREFERENCED_PARAMETER(Parameter);UNREFERENCED_PARAMETER(Timer);//// Do something when the timer fires.//_tprintf(_T("MyTimerCallback: timer has fired.\n"));}//
// This is the thread pool work callback function.
//
VOID
CALLBACK
MyWorkCallback(PTP_CALLBACK_INSTANCE Instance,PVOID                 Parameter,PTP_WORK              Work)
{// Instance, Parameter, and Work not used in this example.UNREFERENCED_PARAMETER(Instance);UNREFERENCED_PARAMETER(Parameter);UNREFERENCED_PARAMETER(Work);BOOL bRet = FALSE;//// Do something when the work callback is invoked.//{_tprintf(_T("MyWorkCallback: Task performed.\n"));}return;
}VOID
DemoCleanupPersistentWorkTimer()
{BOOL bRet = FALSE;PTP_WORK work = NULL;PTP_TIMER timer = NULL;PTP_POOL pool = NULL;PTP_WORK_CALLBACK workcallback = MyWorkCallback;PTP_TIMER_CALLBACK timercallback = MyTimerCallback;TP_CALLBACK_ENVIRON CallBackEnviron;PTP_CLEANUP_GROUP cleanupgroup = NULL;FILETIME FileDueTime;ULARGE_INTEGER ulDueTime;UINT rollback = 0;InitializeThreadpoolEnvironment(&CallBackEnviron);//// Create a custom, dedicated thread pool.//pool = CreateThreadpool(NULL);if (NULL == pool) {_tprintf(_T("CreateThreadpool failed. LastError: %u\n"),GetLastError());goto main_cleanup;}rollback = 1; // pool creation succeeded//// The thread pool is made persistent simply by setting// both the minimum and maximum threads to 1.//SetThreadpoolThreadMaximum(pool, 1);bRet = SetThreadpoolThreadMinimum(pool, 1);if (FALSE == bRet) {_tprintf(_T("SetThreadpoolThreadMinimum failed. LastError: %u\n"),GetLastError());goto main_cleanup;}//// Create a cleanup group for this thread pool.//cleanupgroup = CreateThreadpoolCleanupGroup();if (NULL == cleanupgroup) {_tprintf(_T("CreateThreadpoolCleanupGroup failed. LastError: %u\n"), GetLastError());goto main_cleanup; }rollback = 2;  // Cleanup group creation succeeded//// Associate the callback environment with our thread pool.//SetThreadpoolCallbackPool(&CallBackEnviron, pool);//// Associate the cleanup group with our thread pool.// Objects created with the same callback environment// as the cleanup group become members of the cleanup group.//SetThreadpoolCallbackCleanupGroup(&CallBackEnviron,cleanupgroup,NULL);//// Create work with the callback environment.//work = CreateThreadpoolWork(workcallback,NULL, &CallBackEnviron);if (NULL == work) {_tprintf(_T("CreateThreadpoolWork failed. LastError: %u\n"),GetLastError());goto main_cleanup;}rollback = 3;  // Creation of work succeeded//// Submit the work to the pool. Because this was a pre-allocated// work item (using CreateThreadpoolWork), it is guaranteed to execute.//SubmitThreadpoolWork(work);//// Create a timer with the same callback environment.//timer = CreateThreadpoolTimer(timercallback,NULL,&CallBackEnviron);if (NULL == timer) {_tprintf(_T("CreateThreadpoolTimer failed. LastError: %u\n"),GetLastError());goto main_cleanup;}rollback = 4;  // Timer creation succeeded//// Set the timer to fire in one second.//ulDueTime.QuadPart = (ULONGLONG) -(1 * 10 * 1000 * 1000);FileDueTime.dwHighDateTime = ulDueTime.HighPart;FileDueTime.dwLowDateTime  = ulDueTime.LowPart;SetThreadpoolTimer(timer,&FileDueTime,0,0);//// Delay for the timer to be fired//Sleep(1500);//// Wait for all callbacks to finish.// CloseThreadpoolCleanupGroupMembers also releases objects// that are members of the cleanup group, so it is not necessary // to call close functions on individual objects // after calling CloseThreadpoolCleanupGroupMembers.//CloseThreadpoolCleanupGroupMembers(cleanupgroup,FALSE,NULL);//// Already cleaned up the work item with the// CloseThreadpoolCleanupGroupMembers, so set rollback to 2.//rollback = 2;goto main_cleanup;main_cleanup://// Clean up any individual pieces manually// Notice the fall-through structure of the switch.// Clean up in reverse order.//switch (rollback) {case 4:case 3:// Clean up the cleanup group members.CloseThreadpoolCleanupGroupMembers(cleanupgroup,FALSE, NULL);case 2:// Clean up the cleanup group.CloseThreadpoolCleanupGroup(cleanupgroup);case 1:// Clean up the pool.CloseThreadpool(pool);default:break;}return;
}VOID
DemoNewRegisterWait()
{PTP_WAIT Wait = NULL;PTP_WAIT_CALLBACK waitcallback = MyWaitCallback;HANDLE hEvent = NULL;UINT i = 0;UINT rollback = 0;//// Create an auto-reset event.//hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);if (NULL == hEvent) {// Error Handlingreturn;}rollback = 1; // CreateEvent succeededWait = CreateThreadpoolWait(waitcallback,NULL,NULL);if(NULL == Wait) {_tprintf(_T("CreateThreadpoolWait failed. LastError: %u\n"),GetLastError());goto new_wait_cleanup;}rollback = 2; // CreateThreadpoolWait succeeded//// Need to re-register the event with the wait object// each time before signaling the event to trigger the wait callback.//for (i = 0; i < 5; i ++) {SetThreadpoolWait(Wait,hEvent,NULL);SetEvent(hEvent);//// Delay for the waiter thread to act if necessary.//Sleep(500);//// Block here until the callback function is done executing.//WaitForThreadpoolWaitCallbacks(Wait, FALSE);}new_wait_cleanup:switch (rollback) {case 2:// Unregister the wait by setting the event to NULL.SetThreadpoolWait(Wait, NULL, NULL);// Close the wait.CloseThreadpoolWait(Wait);case 1:// Close the event.CloseHandle(hEvent);default:break;}return;
}int main( void)
{DemoNewRegisterWait();DemoCleanupPersistentWorkTimer();return 0;
}

相關話題

線程池

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448211.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448211.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448211.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

制動剎車片六個養護要點

剎車片屬于消耗品&#xff0c;在使用中會逐漸磨損&#xff0c;當磨損到極限位置時&#xff0c;必須更換&#xff0c;否則將降低制動的效果&#xff0c;甚至造成安全事故。 制動剎車片關乎生命安全&#xff0c;必須謹慎對待。 大多數轎車采用前盤后鼓式制動器結構&#xff0c;一…

Learn day4 函數參數\變量\閉包\遞歸

1.函數描述 # ### 函數 """ (1)函數的定義:功能 (包裹一部分代碼 實現某一個功能 達成某一個目的) (2)函數特點:可以反復調用,提高代碼的復用性,提高開發效率,便于維護管理 """# (3) 函數的基本格式 """ # 函數的定義處 def fun…

Java 中去除字符串中空格的方法

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1、方法分類 str.trim(); //去掉首尾空格str.replace(" ",""); //去除所有空格&#xff0c;包括首尾、中間str.re…

使用重定向的輸入和輸出創建子進程

本主題中的示例演示如何使用控制臺進程中的CreateProcess函數創建子進程。它還演示了一種使用匿名管道重定向子進程的標準輸入和輸出句柄的技術。請注意&#xff0c;命名管道也可用于重定向進程I / O. 所述CreatePipe函數使用SECURITY_ATTRIBUTES結構來創建可繼承句柄讀寫兩個…

手動擋停車時掛檔有技巧

徐小姐來電&#xff1a;我家的汽車要年檢了&#xff0c;前幾天&#xff0c;工作人員幫我把車子開進檢測站去檢測&#xff0c;開回來后停在原位上&#xff0c;然后把鑰匙交給我。我拿鑰匙一點火&#xff0c;車子就突然往前動了&#xff0c;根本沒有時間反應&#xff0c;已經撞到…

LOJ 3156: 「NOI2019」回家路線

題目傳送門&#xff1a;LOJ #3156。 題意簡述&#xff1a; 有一張 \(n\) 個點 \(m\) 條邊的有向圖&#xff0c;邊有兩個權值 \(p_i\) 和 \(q_i\)&#xff08;\(p_i<q_i\)&#xff09;表示若 \(p_i\) 時刻在這條邊的起點&#xff0c;則 \(q_i\) 時刻能到達這條邊的終點。 你需…

線程池概述

線程池 一個線程池的工作線程代表應用程序的高效執行異步回調的集合。線程池主要用于減少應用程序線程的數量并提供工作線程的管理。應用程序可以對工作項進行排隊&#xff0c;將工作與可等待的句柄相關聯&#xff0c;根據計時器自動排隊&#xff0c;并與I / O綁定。 線程池架…

WEB 請求處理二:Nginx 請求 反向代理

上一篇《WEB請求處理一&#xff1a;瀏覽器請求發起處理》&#xff0c;我們講述了瀏覽器端請求發起過程&#xff0c;通過DNS域名解析服務器IP&#xff0c;并建立TCP連接&#xff0c;發送HTTP請求。本文將講述請求到達反向代理服務器的一個處理過程&#xff0c;比如&#xff1a;在…

方向盤的正確駕馭方法

如果問您油門踏板和方向盤哪個與駕駛員最“親密”&#xff0c;您會選擇誰呢&#xff1f;恐怕還是方向盤吧。如果汽車行駛過程中您的雙手同時離開了方向盤&#xff0c;那么事故的隱患也就隨之而來。下面我們就為您全面介紹汽車方向盤的正確使用方法。專家介紹&#xff0c;握方向…

SQL server 2005中無法新建作業(Job)的問題

客戶端是使用企業管理其&#xff08;Management Studio&#xff09;新建job&#xff0c;總是無法創建&#xff0c;查找了很多資料&#xff0c;有的說是需要sp2, 但有的又說不是... ... 沒有時間去研究為什么&#xff0c;但確有一種方法解決&#xff1a;到服務器端去創建job&…

線程池API

線程池API 線程池應用程序編程接口&#xff08;API&#xff09;使用基于對象的設計。以下每個對象都由用戶模式數據結構表示&#xff1a; 池對象是一組可用于執行工作的工作線程。每個進程可以根據需要創建具有不同特征的多個隔離池。每個進程都有一個默認池。清理組與一組回…

WEB 請求處理 一:瀏覽器 請求發起處理

最近&#xff0c;終于要把《WEB請求處理系列》提上日程了&#xff0c;一直答應小伙伴們給分享一套完整的WEB請求處理流程&#xff1a;從瀏覽器、Nginx、Servlet容器&#xff0c;最終到應用程序WEB請求的一個處理流程&#xff0c;前段時間由于其他工作事情的安排&#xff0c;一直…

離合器半聯動探秘

離合器踏板作用是切斷發動機和變速箱之間的動力&#xff0c;有利于起步、變速、和停車。那么如何更好的使用它呢&#xff1f; 離合器的五種狀態示意圖 離合器半聯動的使用方法揭密如下&#xff1a; 離合器半聯動的使用探密之一 將離合器抬到車開始動時你就別再抬了&#xff0c;…

Biztalk Server 2006安裝配置

前段時間收到了來自beta.microsoft.com的BTS20006 Beta2的下載地址&#xff0c;這兩天對它進行了一番安裝配置。下面把一些經過和步驟和大家分享一下&#xff0c;手中有一些去年的Biztalk Server2004版本的培訓資料&#xff0c;里面有11個Lab。需要的朋友請留下mail&#xff0c…

apache 官方 Dubbo 文檔

只是分享、記錄一下 dubbo 的文檔地址&#xff1a;apache 官方 Dubbo 文檔 其頁面內容如下&#xff1a;&#xff08;我是用 chrome 直接右鍵翻譯的&#xff0c;原文檔是英文的&#xff09;

制動踏板是什么?

制動踏板就是腳剎&#xff08;行車制動器&#xff09;的踏板&#xff0c;使運行中的機車、車輛及其他運輸工具或機械等停止或減低速度的動作。制動的一般原理是在機器的高速軸上固定一個輪或盤&#xff0c;在機座上安裝與之相適應的閘瓦、帶或盤&#xff0c;在外力作用下使之產…

CSS Framework 960 Grid System (收)

CSS框架 &#xff1a;960 Grid System 官網&#xff1a;http://960.gs/ 什么是框架&#xff1f;框架是一種你能夠使用在你的web項目中概念上的結構。CSS框架一般是CSS文件的集合&#xff0c;包括基本風格的字體排版&#xff0c;表單樣式&#xff0c;表格布局等等&#xff0c;比…

使用線程本地存儲

線程本地存儲&#xff08;TLS&#xff09;使同一進程的多個線程能夠使用由TlsAlloc函數分配的索引來存儲和檢索線程本地的值。在此示例中&#xff0c;在進程啟動時分配索引。當每個線程啟動時&#xff0c;它會分配一個動態內存塊&#xff0c;并使用TlsSetValue函數在TLS槽中存儲…

發動機的工作原理,你知道嗎?

http://auto.jxedt.com/info/5352.htm 發動機是汽車的動力裝置&#xff0c;性能優劣直接影響到汽車性能&#xff0c;發動機的類型很多&#xff0c;結構各異&#xff0c;以適應不同車型的需要。按發動機使用燃料劃分&#xff0c;可分成汽油發動機和柴油發動機等類別。按發動機汽…

官方文檔: Dubbo 框架設計、模塊說明、依賴關系

以下內容全文轉自 apache 官方 dubbo文檔&#xff1a;http://dubbo.apache.org/en-us/docs/dev/design.html 框架設計 圖片描述&#xff1a; 淺藍色背景的左側區域顯示服務用戶界面&#xff0c;淺綠色背景的右側區域顯示服務提供者界面&#xff0c;中心區域顯示兩個側面界面。…