关于回调函数,该怎么解决

关于回调函数
回调函数是什么啊   回调函数有什么意义啊  为什么我看了网上的介绍 感觉回调函数的作用跟  c++里的多态
一样啊 关于回调函数,该怎么解决   最好能举个例子 告诉我用回调函数 和 不用回调函数的区别   大神们先谢谢了关于回调函数,该怎么解决

------解决方案--------------------
程序模块(比如两个类对象)之间需要相互通信,比如a和b,a需要在b达到某个条件的时候做一些事情。通常有两种方法:

1)a主动查询b
2)b在达到条件的时候通知a

前者叫做轮询(poll),是一种主动通信。后者叫做回调(callback),是一种被动通信。

在poll情况下,因为a不知道b什么时候达到条件,因此如果a需要对b迅速反应,就必须在每帧查询b的条件,因此大量的cpu周期被浪费在无谓的查询中。但这种模型编程比较容易。

在callback情况下,a不需要主动查询,a只需要做自己的事情就行了,b在自己达到条件的时候,会主动的通知a,因此a不会浪费cpu周期。但这种模型编程相对poll复杂。

如果想更深入的了解他们,可以参考

http://blog.csdn.net/popy007/article/details/8242787

看里面的通知模型部分就可以了。讨论的很具体。
------解决方案--------------------
Windows系统:“不要调用我,请先填写好未来收到某个消息时你的处理流程,在那个消息到来时我会调用你!”
#pragma comment(lib,"user32")
#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>
#include <windows.h>
char datestr[16];
char timestr[16];
char mss[4];
void log(char *s) {
    struct tm *now;
    struct timeb tb;

    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,s);
}
VOID CALLBACK myTimerProc1(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc1\n");
}
VOID CALLBACK myTimerProc2(
  HWND hwnd, // handle of window for timer messages
  UINT uMsg, // WM_TIMER message
  UINT idEvent, // timer identifier
  DWORD dwTime // current system time
) {
 log("In myTimerProc2\n");
}
int main() {
    int i;
    MSG msg;

    SetTimer(NULL,0,1000,myTimerProc1);
    SetTimer(NULL,0,2000,myTimerProc2);
    for (i=0;i<20;i++) {
        Sleep(500);
        log("In main\n");
        if (GetMessage(&msg,NULL,0,0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

    }
    return 0;
}
//2012-07-26 17:29:06.375 In main
//2012-07-26 17:29:06.875 In myTimerProc1
//2012-07-26 17:29:07.375 In main
//2012-07-26 17:29:07.875 In myTimerProc2