#ifndef _TASKTIMER_H
#define _TASKTIMER_H
#include <iostream>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <pthread.h>
#endif
//定义回调函数
typedef void (*timerCallBack)(void* param);
class TaskTimer
{
public:
TaskTimer(int timeSecond);
~TaskTimer(void);
public:
void start();
void stop();
void setTimerFun(timerCallBack fun, void* param); //注册回调函数
static void * timer(void* context); //线程函数
public:
timerCallBack m_timerFun;
void* m_pFunParam;
public:
int m_timeSecond; //线程执行时间
bool m_bTimerStop; //时间设置状态
static unsigned long getTickCount(void);
};
#endif
#include <stdio.h>
#include "TaskTimer.h"
TaskTimer::TaskTimer(int timeSecond)
{
std::cout << "exec taskTime()" << std::endl;
m_timeSecond = timeSecond; //秒
m_bTimerStop = false;
m_timerFun = NULL;
m_pFunParam = NULL;
}
TaskTimer::~TaskTimer(void)
{
std::cout << "exec ~ taskTime()" << std::endl;
stop();
}
//创建线程执行定时函数
void TaskTimer::start()
{
pthread_t ptid = 0;
int ret = pthread_create(&ptid, NULL,TaskTimer::timer, (void*)this);
}
void TaskTimer::stop()
{
m_bTimerStop = true;
}
//设置回调函数
void TaskTimer::setTimerFun(timerCallBack fun, void* param)
{
m_timerFun = fun;
m_pFunParam = param;
}
//定时器线程
void * TaskTimer::timer(void* context)
{
TaskTimer* pthis = (TaskTimer*)context;
if (pthis == NULL)
return NULL;
//当前时间
unsigned int curTm = 0;
//记录上一次时间
unsigned int lastTm = 0;
//当对象未被销毁调用回调函数
while (!pthis->m_bTimerStop)
{
curTm = getTickCount();
if (curTm - lastTm >= pthis->m_timeSecond*1000)
{
lastTm = curTm;
}
else
{
usleep(1000*1000);
continue;
}
//执行定时器函数
if (pthis->m_timerFun != NULL)
{
pthis->m_timerFun(pthis->m_pFunParam);
}
}
return NULL;
}
unsigned long TaskTimer::getTickCount(void)
{
unsigned long curTime;
#ifdef WIN32
curTime = GetTickCount();
#else
//获取当前时间毫秒级别
struct timeval current;
gettimeofday(¤t, NULL);
curTime = current.tv_sec * 1000 + current.tv_usec/1000;
#endif
return curTime;
}
#include <stdio.h>
#include "TaskTimer.h"
void directoryGetProc(void* param);
TaskTimer* m_pCatalogTimer = new TaskTimer(3);
int main()
{
//TaskTimer* m_pCatalogTimer = new TaskTimer(3);
printf("==============\n");
m_pCatalogTimer->setTimerFun(directoryGetProc, NULL);
m_pCatalogTimer->start();
sleep(10);
m_pCatalogTimer->stop();
printf("**** TaskTimer is stop ****.\n");
delete(m_pCatalogTimer);
sleep(1);
printf("****main exit****.\n");
return 0;
}
void directoryGetProc(void* param)
{
std::string src = " test for TimerCallback";
unsigned int curTm = 0;
curTm = m_pCatalogTimer->getTickCount();
unsigned long curTime;
struct timeval current;
gettimeofday(¤t, NULL);
curTime = current.tv_sec * 1000 + current.tv_usec / 1000;
//printf("%s\n",src);
std::cout<< "Time:" << curTime << src << std::endl;
//std::cout<< "Time:" << curTm << src << std::endl;
}
huqin@ubuntu:/mnt/hgfs/share/code/2021/mon11/week1/time定时器$ g++ TaskTimer.cpp main.cpp -o taskTimer -lpthread
huqin@ubuntu:/mnt/hgfs/share/code/2021/mon11/week1/time定时器$ ./taskTimer
exec taskTime()
==============
Time:4252011832 test for TimerCallback //这里显示的是毫秒级
Time:4252014835 test for TimerCallback // (4252014835 - 4252011832)%1000 = 3s
Time:4252017838 test for TimerCallback
Time:4252020839 test for TimerCallback
**** TaskTimer is stop ****.
exec ~ taskTime()
thread exit
****main exit****.