定时器的简单实现即回调函数的运用
来源:互联网 发布:超声波洗菜机 知乎 编辑:程序博客网 时间:2024/05/28 16:14
这两天在 研究回调函数就想实现简单的定时器,以下是鄙人的程序望指教。
#include <iostream>
#include <ctime>
using namespace std;
#define MAXNUM 256
typedef void (*timerProcessFunc)(void*);
typedef struct
{
unsigned int id;
int timeout; //毫秒
}MyTimer;
static MyTimer timerList[MAXNUM] = {0};
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;
}
void timerProcess(void* userPara) //回调函数
{
cout << "定时了" << *(double*)userPara << "毫秒" << endl;
}
void startTimer(int timerID, timerProcessFunc timerapp)
{
clock_t start,finish;
double totaltime;
start = clock();
/**********计时开始*****************/
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;
}
- 定时器的简单实现即回调函数的运用
- 回调函数的运用
- 什么是回调函数?回调函数的简单实现。
- Spine的回调函数,以及简单运用
- 简单的回调函数实现
- java回调函数的简单实现
- 回调函数的理解及运用
- C++回调函数(callback)的运用
- js中回调函数的运用
- 简单的回调函数
- 一个简单的java回调函数的实现
- C++的回调函数的简单实现
- PJSIP的DTMF简单实现,回调函数:on_dtmf_digit
- 使用回调函数实现一个简单的计算器;
- C++ 对回调函数的理解与运用
- java回调函数的作用以及运用
- 多线程 Semaphore 回调函数 多媒体定时器的使用
- 使用HalTimerConfig进不了定时器回调函数的解决方法
- linux(六)
- 组件的使用(二)ListView的使用
- Opencv::findContours函数参数说明及相关函数
- leetcode_171 Excel Sheet Column Number & leetcode_28 Implement strStr()
- 动态规划之子序列与子串问题分析
- 定时器的简单实现即回调函数的运用
- minHash(最小哈希)和LSH(局部敏感哈希)
- 利用keepalived实现双台服务器主备高可用
- HDOJ 2544 最短路(最短路径 dijkstra算法,SPFA邻接表实现,floyd算法)
- Ansible
- linux(七)
- 1079. Total Sales of Supply Chain (25)
- QObject 出现undefined vtable的解决方法
- Android-异步HttpClient框架(get/post)