timer_create的简单使用

一、SIGEV_SIGNAL通知方式

当时间到达时,发送一个信号

#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>


void sigusr1_handler(int sig)
{
	printf("%s---%d\n", __func__, __LINE__);

	return ;
}

int main(void)
{
	int ret;
	timer_t timer_id;
	struct sigevent sev;
	struct itimerspec its;

	signal(SIGUSR1, sigusr1_handler);
	
	sev.sigev_notify = SIGEV_SIGNAL;
	sev.sigev_signo = SIGUSR1;
	sev.sigev_value.sival_ptr = &timer_id;
	ret = timer_create(CLOCK_REALTIME, &sev, &timer_id);
	
	its.it_value.tv_sec = 1;
	its.it_value.tv_nsec = 0;
	its.it_interval.tv_sec = 1;
	its.it_interval.tv_nsec = 0;
	timer_settime(timer_id, 0, &its, NULL);
	
	sleep(2);
	sleep(2);
	
	return 0;
}

二、SIGEV_THREAD通知方式

当时间到达时,创建一个线程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>


void sigalrm_handler(int sig)
{
	printf("%s---%d\n", __func__, __LINE__);
	
	return ;
}



int main(int argc, char *argv[])
{
	int ret;
	timer_t timer_id;
	struct sigevent sev;
	struct itimerspec its;
	
	memset(&sev, 0, sizeof(struct sigevent));
	sev.sigev_notify = SIGEV_THREAD;
	sev.sigev_notify_function = thread_start;
	sev.sigev_value.sival_ptr = &timer_id;
	sev.sigev_value.sival_int = 3;
	ret = timer_create(CLOCK_REALTIME, &sev, &timer_id);
	
	its.it_value.tv_sec = 1;
	its.it_value.tv_nsec = 0;
	its.it_interval.tv_sec = 1;
	its.it_interval.tv_nsec = 0;
	timer_settime(timer_id, TIMER_ABSTIME, &its, NULL);
	
	sleep(10);
	
	
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux平台上使用timer_create函数可以创建一个定时器。在Delphi中,可以通过调用Linux系统库头文件来实现该函数的调用。下面是一个简单的示例代码: ```delphi unit LinuxTimer; interface uses LinuxApi; type TTimer = class private FTimerID: timer_t; FInterval: Cardinal; FOnTimer: TNotifyEvent; FIsStarted: Boolean; FTimerSpec: itimerspec; procedure SetInterval(const Value: Cardinal); public constructor Create; destructor Destroy; override; procedure Start; procedure Stop; property Interval: Cardinal read FInterval write SetInterval; property OnTimer: TNotifyEvent read FOnTimer write FOnTimer; property IsStarted: Boolean read FIsStarted; end; implementation uses SysUtils; { TTimer } constructor TTimer.Create; begin inherited; FTimerID := 0; FInterval := 1000; FIsStarted := False; end; destructor TTimer.Destroy; begin Stop; inherited; end; procedure TTimer.SetInterval(const Value: Cardinal); begin if FInterval <> Value then begin FInterval := Value; if IsStarted then begin Stop; Start; end; end; end; procedure TTimer.Start; begin if not IsStarted then begin FillChar(FTimerSpec, SizeOf(FTimerSpec), 0); FTimerSpec.it_interval.tv_sec := FInterval div 1000; FTimerSpec.it_interval.tv_nsec := (FInterval mod 1000) * 1000000; FTimerSpec.it_value.tv_sec := FInterval div 1000; FTimerSpec.it_value.tv_nsec := (FInterval mod 1000) * 1000000; if timer_create(CLOCK_REALTIME, nil, @FTimerID) = 0 then begin if timer_settime(FTimerID, 0, @FTimerSpec, nil) = 0 then FIsStarted := True else timer_delete(FTimerID); end; end; end; procedure TTimer.Stop; begin if IsStarted then begin FTimerSpec.it_interval.tv_sec := 0; FTimerSpec.it_interval.tv_nsec := 0; FTimerSpec.it_value.tv_sec := 0; FTimerSpec.it_value.tv_nsec := 0; timer_settime(FTimerID, 0, @FTimerSpec, nil); timer_delete(FTimerID); FIsStarted := False; end; end; end. ``` 这是一个简单的封装,使用时只需要创建一个TTimer对象,并调用Start方法就可以启动一个定时器。在该代码中,使用Linux API中的timer_createtimer_settime函数来实现定时器功能。同时,该封装使用Delphi中的事件模型,通过设置OnTimer事件,在定时器到期时,会触发该事件,从而完成定时器任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值