Linux使用线程实现定时器

#include <sys/time.h>
#include <stdio.h>
#include <pthread.h>
#include <functional>
#include <stdlib.h>
#include <unistd.h>


static long long  last_timestap;
using std::placeholders::_1;
int work()
{
    struct timeval time;
    gettimeofday(&time,NULL);
    printf("间隔=%lldus\n",((long long)(time.tv_sec*1000*1000 + time.tv_usec)-last_timestap));
    last_timestap=time.tv_sec*1000*1000 + time.tv_usec;
}
// 计数器函数,需要每隔指定时间调用
void count(int c)
{
    static int count_num=0;
    count_num++;
    if(count_num==10)
    {
        work();
        count_num=0;
    }
}


long long g_usec;
std::function<void(int)> g_func;
void startTimer(std::function<void(int)> func,int usec)
{
    pthread_t th_t;
    g_usec=usec;
    g_func=func;
    pthread_create(&th_t,NULL,[](void*)->void*{
        for(;;)
        {
            usleep(g_usec-60);
            g_func(1);
        }
    },NULL);
}

int main()
{
    auto func=std::bind(count,_1);
    startTimer(func,1*1000*1000);
    for(;;){}
    return 0;
}
带误差修正版本
#include <sys/time.h>
#include <stdio.h>
#include <pthread.h>
#include <functional>
#include <stdlib.h>
#include <unistd.h>

static long long  last_timestap;
using std::placeholders::_1;
int work()
{
    struct timeval time;
    gettimeofday(&time,NULL);
    printf("间隔=%lldus\n",((long long)(time.tv_sec*1000*1000 + time.tv_usec)-last_timestap));
    last_timestap=time.tv_sec*1000*1000 + time.tv_usec;
}
// 计数器函数,需要每隔指定时间调用
void count(int c)
{
    static int count_num=0;
    count_num++;
    if(count_num==10)
    {
        work();
        count_num=0;
    }
}


long long g_usec;
std::function<void(int)> g_func;
long long g_last=0;
long long g_timestap=0;
long long g_s_usec=0;
long long g_devi=200;  // 0.2ms
void startTimer(std::function<void(int)> func,int usec)
{
    pthread_t th_t;
    g_s_usec=usec;
    g_func=func;
    g_usec=usec;
    pthread_create(&th_t,NULL,[](void*)->void*{
        for(;;)
        {
            struct timeval time;
            gettimeofday(&time,NULL);
            g_last=time.tv_usec+time.tv_sec*1000*1000;
            usleep(g_usec);
            g_func(1);
            gettimeofday(&time,NULL);
            g_timestap=time.tv_usec+time.tv_sec*1000*1000;
            if(g_timestap - g_last - g_usec > g_devi)
            {
                g_usec = g_usec - (g_timestap - g_last - g_usec);
            }else{
                g_usec=g_s_usec;
            }
        }
    },NULL);
}

int main()
{
    auto func=std::bind(count,_1);
    startTimer(func,10*1000);
    for(;;){}
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以使用线程实现定时器功能。一种常见的方法是使用`timer_create`函数创建一个定时器对象,然后使用`timer_settime`函数来设置定时器的触发时间和间隔。在定时器触发时,系统会发送一个信号给指定的线程,该线程可以在信号处理函数中执行相应的操作。 以下是一个简单的示例代码,演示了如何在多线程使用定时器: ```c #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <pthread.h> #include <time.h> #define TIMER_INTERVAL_SEC 1 void timer_handler(int sig) { // 定时器触发时执行的操作 printf("Timer expired\n"); } void* thread_func(void* arg) { // 创建定时器 timer_t timer; struct sigevent sev; struct itimerspec its; sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGALRM; sev.sigev_value.sival_ptr = &timer; timer_create(CLOCK_REALTIME, &sev, &timer); // 设置定时器的触发时间和间隔 its.it_value.tv_sec = TIMER_INTERVAL_SEC; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = TIMER_INTERVAL_SEC; its.it_interval.tv_nsec = 0; timer_settime(timer, 0, &its, NULL); // 等待定时器触发 while (1) { sleep(1); } return NULL; } int main() { pthread_t thread; // 创建线程 pthread_create(&thread, NULL, thread_func, NULL); // 等待线程结束 pthread_join(thread, NULL); return 0; } ``` 在上面的代码中,主线程创建了一个子线程,并在子线程中创建了一个定时器定时器的触发时间被设置为1秒,当定时器触发时,会执行`timer_handler`函数中的操作。主线程使用`pthread_join`函数等待子线程结束。 请注意,上述示例中的代码仅供参考,并未处理一些潜在的错误情况。在实际应用中,您可能需要添加适当的错误处理和线程同步机制以确保程序的正确性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值