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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

线程通信机制之定时器队列

發(fā)布時(shí)間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程通信机制之定时器队列 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

線程通信機(jī)制之定時(shí)器隊(duì)列

——每周雜談 第008篇

作者:Tocy????時(shí)間:2012-06-01

關(guān)鍵詞:定時(shí)器隊(duì)列,ITC

定時(shí)器隊(duì)列(Timer Queue)可以使用CreateTimerQueue函數(shù)創(chuàng)建。定時(shí)器隊(duì)列中的定時(shí)器是輕量級(jí)對(duì)象,可以在一定時(shí)間間隔之后調(diào)用指定的回調(diào)函數(shù)(可以只調(diào)用一次,也可以是周期性的)。這種等待操作由線程池中某個(gè)線程處理的(系統(tǒng)級(jí)別)。

向定時(shí)器隊(duì)列中添加定時(shí)器可以使用CreateTimerQueueTimer函數(shù)。更新一個(gè)計(jì)時(shí)器隊(duì)列中的計(jì)時(shí)器可以使用?ChangeTimerQueueTimer?函數(shù)。這兩個(gè)函數(shù)中你可以指定一個(gè)回調(diào)函數(shù),如果設(shè)定時(shí)間到達(dá),線程池中某個(gè)線程會(huì)調(diào)用該回調(diào)函數(shù)。

使用?DeleteTimerQueueTimer函數(shù)可以取消掛起的計(jì)時(shí)器。當(dāng)不再使計(jì)時(shí)器隊(duì)列的時(shí)候,調(diào)用?DeleteTimerQueueEx?函數(shù)刪除計(jì)時(shí)器隊(duì)列,該函數(shù)調(diào)用是會(huì)取消并刪除任何在該隊(duì)列中等待的計(jì)時(shí)器。

MSDN鏈接:http://msdn.microsoft.com/en-us/library/ms686796(v=vs.85)

使用例子如下:

#include <windows.h>

#include <stdio.h>

?

HANDLE gDoneEvent;

?

VOID CALLBACK TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired)

{

if (lpParam == NULL)

{

printf("TimerRoutine lpParam is NULL\n");

}

else

{

// lpParam points to the argument; in this case it is an int

?

printf("Timer routine called. Parameter is %d.\n",

*(int*)lpParam);

if(TimerOrWaitFired)

{

printf("The wait timed out.\n");

}

else

{

printf("The wait event was signaled.\n");

}

}

?

SetEvent(gDoneEvent);

}

?

int main()

{

HANDLE hTimer = NULL;

HANDLE hTimerQueue = NULL;

int arg = 123;

?

// Use an event object to track the TimerRoutine execution

gDoneEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (NULL == gDoneEvent)

{

printf("CreateEvent failed (%d)\n", GetLastError());

return 1;

}

?

// Create the timer queue.

hTimerQueue = CreateTimerQueue();

if (NULL == hTimerQueue)

{

printf("CreateTimerQueue failed (%d)\n", GetLastError());

return 2;

}

?

// Set a timer to call the timer routine in 10 seconds.

if (!CreateTimerQueueTimer( &hTimer, hTimerQueue,

(WAITORTIMERCALLBACK)TimerRoutine, &arg , 10000, 0, 0))

{

printf("CreateTimerQueueTimer failed (%d)\n", GetLastError());

return 3;

}

?

// TODO: Do other useful work here

?

printf("Call timer routine in 10 seconds...\n");

?

// Wait for the timer-queue thread to complete using an event

// object. The thread will signal the event at that time.

?

if (WaitForSingleObject(gDoneEvent, INFINITE) != WAIT_OBJECT_0)

printf("WaitForSingleObject failed (%d)\n", GetLastError());

?

CloseHandle(gDoneEvent);

?

// Delete all timers in the timer queue.

if (!DeleteTimerQueue(hTimerQueue))

printf("DeleteTimerQueue failed (%d)\n", GetLastError());

?

return 0;

}

這段代碼比較容易理解,這里不做詳細(xì)解釋了。功能很簡(jiǎn)單:利用定時(shí)器隊(duì)列實(shí)現(xiàn)延時(shí)10s執(zhí)行指定回調(diào)函數(shù)。

比較有意思的是:CreateTimerQueueTimer函數(shù)。

該函數(shù)功能是創(chuàng)建一個(gè)計(jì)時(shí)器隊(duì)列中的計(jì)時(shí)器。原型如下:

BOOL WINAPI CreateTimerQueueTimer(

__out?????????PHANDLE phNewTimer,????????????????//計(jì)時(shí)器句柄指針

__in_opt??????HANDLE TimerQueue,????????????????//計(jì)時(shí)器隊(duì)列句柄

__in??????????WAITORTIMERCALLBACK Callback,????????//回調(diào)函數(shù)指針

__in_opt?????PVOID Parameter,????????????????????//傳給回調(diào)函數(shù)的參數(shù)

__in??????????DWORD DueTime,????????????????????//到期時(shí)間(以毫秒為單位)

__in??????????DWORD Period,????????????????????//計(jì)時(shí)器周期(以毫秒為單位)

__in??????????ULONG Flags????????????????????????//標(biāo)志位,通常使用0

);

計(jì)時(shí)器在DueTime時(shí)間過(guò)后首次到期,然后按照給定的period時(shí)間間隔到期,每次到期都會(huì)調(diào)用回調(diào)函數(shù)。

其參數(shù)中計(jì)時(shí)器隊(duì)列指針可以為NULL(此時(shí)計(jì)時(shí)器與默認(rèn)計(jì)時(shí)器隊(duì)列關(guān)聯(lián))。period如果為0,計(jì)時(shí)器指signaled一次;如果大于0,計(jì)時(shí)器是周期性的,直到計(jì)時(shí)器被取消。

?

Windows下的計(jì)時(shí)器有三類:Timer,timer queue,和waitable timer。從復(fù)雜度上而言,一次增加,功能也更加強(qiáng)大。Timer只能用在單獨(dú)一個(gè)計(jì)時(shí)器處理函數(shù)或者WM_TIMER消息中,而timer queue可以提供更多更準(zhǔn)確的計(jì)時(shí)方式(在線程池中運(yùn)行,效率更高), waitable timer則是同步對(duì)象,可以用于多進(jìn)程之間的通信。從功能上而言基本都是一致的,只有timer提供簡(jiǎn)單的周期執(zhí)行(沒(méi)有單次執(zhí)行的計(jì)時(shí)器,當(dāng)然可以實(shí)現(xiàn)這種計(jì)時(shí)器的)。

所以如果需要使用timer (計(jì)時(shí)器)的話,最好從最簡(jiǎn)單的開始,如果性能不能滿足要求,依次嘗試timer、timer queue、waitable timer。

?

注:版權(quán)所有,請(qǐng)勿用于商業(yè)用途,轉(zhuǎn)載請(qǐng)注明原文地址。本人保留所有權(quán)利。

轉(zhuǎn)載于:https://www.cnblogs.com/tocy/archive/2012/06/03/2532562.html

總結(jié)

以上是生活随笔為你收集整理的线程通信机制之定时器队列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。