定时回调函数类

43 篇文章 1 订阅

#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(&current, 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(&current, 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****.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值