C++ 定时器触发

c++定时器,能够定时触发,即每隔一段固定时间执行一下函数

#include <iostream>
#include <thread>
#include <chrono>
#include <signal.h>
#include <time.h>
#include <cstring>
#include <glog/logging.h>

#define EVENTSAVERTIMER_SIG (SIGRTMIN + 14)   // 设置信号
#define EvensTimerPeriod (5) // 5ms

// 定时器处理函数
void timerHandler(int sig, siginfo_t *si, void *uc) {
    LOG(ERROR) << "Timer triggered!" << std::endl;
}

void EventSaverTimerInit(void)
{

	/*配置一个Posix timer*/
	timer_t TimerPulse;
	struct sigevent Timer1_Pulse_Sig;
	struct sigaction Timer1_Pulse_Sa;
	struct itimerspec Timer1_Pulse_it; // 匹配pulse类型定时器1 的timer设定参数
	int res;

	Timer1_Pulse_Sa.sa_flags = SA_SIGINFO | SA_RESTART;
	Timer1_Pulse_Sa.sa_sigaction = timerHandler;
	sigemptyset(&Timer1_Pulse_Sa.sa_mask);
	if (sigaction(EVENTSAVERTIMER_SIG, &Timer1_Pulse_Sa, NULL) == -1)
	{
		perror("sigaction");
	}

	memset(&Timer1_Pulse_Sig, 0, sizeof(Timer1_Pulse_Sig));

	// 信号量配置
	Timer1_Pulse_Sig.sigev_value.sival_ptr = &TimerPulse;
	Timer1_Pulse_Sig.sigev_notify = SIGEV_SIGNAL;
	Timer1_Pulse_Sig.sigev_signo = EVENTSAVERTIMER_SIG;

	res = timer_create(CLOCK_REALTIME, &Timer1_Pulse_Sig, &TimerPulse);
	if (res != 0)
	{
		perror("TimerPulse create Error");
		return;
	}

	Timer1_Pulse_it.it_value.tv_sec = 0; // 定时器第一次触发的时间, 启动延时时间 5 ms
	Timer1_Pulse_it.it_value.tv_nsec = 5 * 1000 * 1000;
	Timer1_Pulse_it.it_interval.tv_sec = 0;								  // timer周期
	Timer1_Pulse_it.it_interval.tv_nsec = EvensTimerPeriod * 1000 * 1000; // 10 ms, 纳秒,微秒,毫秒,秒
	/*结束配置一个Poisix timer*/

	res = timer_settime(TimerPulse, 0, &Timer1_Pulse_it, NULL);
	if (res)
	{
		perror("TimerPulse settime Error");
		return;
	}
}

int main() {
    // startTimer();
    EventSaverTimerInit();

    // 主线程继续执行其他操作
    for (int i = 0; i < 100; ++i) {
        // std::cout << "Main thread doing work: " << i << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    // 关闭glog
    google::ShutdownGoogleLogging();
    return 0;
}

编译

g++ test.cpp -lrt -lglog

相关推荐

  1. C++ 定时器触发

    2024-07-12 00:24:01       24 阅读
  2. vue3 uniapp定时器 每天定时触发任务

    2024-07-12 00:24:01       57 阅读
  3. C++| QT定时器QTimer

    2024-07-12 00:24:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 00:24:01       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 00:24:01       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 00:24:01       57 阅读
  4. Python语言-面向对象

    2024-07-12 00:24:01       68 阅读

热门阅读

  1. SqlSugar分表笔记

    2024-07-12 00:24:01       25 阅读
  2. 模板语法指令语法——02

    2024-07-12 00:24:01       21 阅读
  3. LeetCode 算法:实现 Trie (前缀树) c++

    2024-07-12 00:24:01       21 阅读
  4. 周报 | 24.7.1-24.7.7文章汇总

    2024-07-12 00:24:01       20 阅读
  5. httpclient访问https请求报错处理

    2024-07-12 00:24:01       19 阅读
  6. 力扣---41. 缺失的第一个正数

    2024-07-12 00:24:01       23 阅读
  7. 微信小程序之使用上拉加载实现图片懒加载

    2024-07-12 00:24:01       24 阅读
  8. C++ --> 类和对象(一)

    2024-07-12 00:24:01       21 阅读
  9. 系统架构的基础:定义、原则与发展历程

    2024-07-12 00:24:01       22 阅读