<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

统计

  • 随笔 - 24
  • 文章 - 0
  • 评论 - 17
  • 引用 - 0

常用链接

留言簿(4)

随笔分类

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

定时器处理函数TimerProc会不会重入?
我觉得不会。另外,我在编程中也似乎从来没考虑过重入的问题。

证明如下:
 1// testtimer.cpp : 定义控制台应用程序的入口点。
 2//
 3
 4#include "stdafx.h"
 5#include <windows.h>
 6#include <conio.h>
 7
 8static UINT idTimer = 0;
 9static int reentry = 0;
10static int call_cnt = 0;
11
12void LengthyWork(void)
13{
14    //Sleep(3000);
15    int i = 0,j = 0;
16    for(i;i < 50000;) {
17        i++;
18        for(j = i;j > 0;) {
19            j--;
20        }

21    }

22
23}

24VOID CALLBACK OnTimer(HWND hwnd,
25    UINT uMsg,
26    UINT_PTR idEvent,
27    DWORD dwTime
28)
29{
30    ++call_cnt;
31    printf("entry(%d)  reentry:%d\n",call_cnt,reentry);
32    ++reentry;
33    LengthyWork();
34    --reentry;
35    printf("exit(%d)   reentry:%d\n",call_cnt,reentry);
36}

37int _tmain(int argc, _TCHAR* argv[])
38{
39    idTimer = SetTimer(NULL,0,1000,OnTimer);
40    int ret = 0;
41    MSG msg;
42    while(1{
43        if(kbhit()) {
44            return 0;
45        }

46        ret = GetMessage(&msg,NULL,0,0);
47        if(ret) {
48            TranslateMessage(&msg);
49            DispatchMessage(&msg);
50        }

51    }

52    return 0;
53}

54

一次执行结果如下:
entry(1)  reentry:0
exit(1)   reentry:0
entry(2)  reentry:0
exit(2)   reentry:0
entry(3)  reentry:0
exit(3)   reentry:0
entry(4)  reentry:0
exit(4)   reentry:0

posted on 2006-09-01 14:48 小葱蘸酱 阅读(1721) 评论(1)  编辑 收藏 引用

评论

# re: 定时器处理函数TimerProc会不会重入? 2012-02-06 19:16 千里马雕刻师傅

会重入的,你把SetTimer(NULL,0,1000,OnTimer)改成
SetTimer(NULL,0,1,OnTimer)试试
  回复  更多评论    

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理