c语言定时器回调函数的参数,定时器的简单实现即回调函数的运用
這兩天在 研究回調(diào)函數(shù)就想實現(xiàn)簡單的定時器,如下是鄙人的程序望指教。ios
#include
#include
using namespace std;app
#define MAXNUM 256函數(shù)
typedef void (*timerProcessFunc)(void*);spa
typedef struct
{
unsigned int id;
int timeout;? //毫秒
}MyTimer;回調(diào)函數(shù)
static MyTimer timerList[MAXNUM] = {0};it
int initTimer(MyTimer* timer, int timeout)
{
if(!timer || timeout < 0) return false;
timer->timeout = timeout;
for(int i = 0; i < MAXNUM; i++)
{
if(timerList[i].id == 0)
{
timer->id = i;
timerList[i] = *timer;
return i;
}
}
return -1;
}io
void timerProcess(void* userPara)? //回調(diào)函數(shù)
{
cout << "定時了" << *(double*)userPara << "毫秒" << endl;
}stream
void startTimer(int timerID, timerProcessFunc timerapp)
{
clock_t start,finish;
double totaltime;
start = clock();List
/**********計時開始*****************/
while(1)
{
finish = clock();
totaltime = (double)(finish-start);
if(totaltime >= timerList[timerID].timeout)
{
timerapp(&totaltime);
break;
}
}
/********************************/
} 定時器
void killTimer(int timerID)
{
timerList[timerID].id = 0;
timerList[timerID].timeout = 0;
}
int main()
{
MyTimer t;
int id;
if((id = initTimer(&t, 10000)) != -1)
startTimer(id, timerProcess);
return 0;
}
總結
以上是生活随笔為你收集整理的c语言定时器回调函数的参数,定时器的简单实现即回调函数的运用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nexus批量上传与下载
- 下一篇: 解决:error C1083: 无法打开