国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

利用linux的timerfd_create實現計時器示例分享

2019-10-26 18:38:40
字體:
來源:轉載
供稿:網友

timer_poll.h
代碼如下:
/*
 * File:   timer_poll.h
 * Author: Administrator
 */

#ifndef TIMER_POLL_H
#define TIMER_POLL_H
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/epoll.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include <pthread.h>
#include <map>

#define MAXFDS 128
#define EVENTS 100
class timer;
typedef int(*timer_callback)(timer &);//user callback

class timer
{
public:

    timer() : timer_internal(0.0), cb(0), timer_id(0), repeat(0), userdata(0){}
    timer(double internal_value, int  (*callback)(timer &ptimer), void *data, int rep) : timer_internal(internal_value), cb(callback), userdata(data), repeat(rep)
    {
        timer_id = timerfd_create(CLOCK_REALTIME, 0);
        setNonBlock(timer_id);
    }

    timer(const timer &ptimer);
    timer & operator=(const timer &ptimer);
    int timer_start();
    int timer_stop();
    int timer_modify_internal(double timer_internal);

    int timer_get_id()
    {
        return timer_id;
    }

    void *timer_get_userdata()
    {
        return userdata;
    }

    timer_callback get_user_callback()
    {
        return cb;
    }

    ~timer()
    {
        timer_stop();
    }

private:

    bool setNonBlock (int fd)
    {
        int flags = fcntl (fd, F_GETFL, 0);
        flags |= O_NONBLOCK;
        if (-1 == fcntl (fd, F_SETFL, flags))
        {
            return false;
        }
        return true;
    }
    int     timer_id;
    double  timer_internal;
    void    *userdata;
    bool    repeat;//will the timer repeat or only once
    timer_callback cb;
} ;
class timers_poll
{
public:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 根河市| 工布江达县| 娱乐| 丰都县| 东安县| 清徐县| 江油市| 南岸区| 栾城县| 丰宁| 怀宁县| 浦东新区| 黔南| 武山县| 张北县| 浦江县| 平利县| 宽城| 鄂尔多斯市| 汝南县| 永春县| 高密市| 湘潭县| 上饶市| 利川市| 靖边县| 会理县| 格尔木市| 高平市| 九寨沟县| 六盘水市| 比如县| 时尚| 中江县| 资兴市| 巴彦淖尔市| 淮南市| 清水县| 蓬溪县| 黎川县| 杭锦后旗|