Original URL: http://blog.csdn.net/hongszh/article/details/8608781
The most powerful timer interface from the POSIX clock series, its creation, initialization, and removal of a timer action is divided into three different functions: Timer_create () (Create timer), Timer_settime () (initialization timer), and Timer_delete (Destroy it).
Man Timer_create/timer_settime, you can see the detailed documentation for man help:
[CPP]View Plaincopy
- Timer_create (2) Linux Programmer ' s Manual
- NAME
- Timer_create-create a POSIX per-process timer
- Synopsis
- #include <signal.h>
- #include <time.h>
- int timer_create (clockid_t clockid, struct sigevent *sevp,
- timer_t *timerid);
- int Timer_settime (timer_t timerid, int flags,
- const struct itimerspec *new_value,
- struct Itimerspec * old_value);
- int Timer_gettime (timer_t timerid, struct itimerspec *curr_value);
My implementation is as follows:
1. Define the function pointer type for timer timeout:
[CPP]View Plaincopy
- typedef VOID (*timertimeout) (Union sigval SIG);
2. Define a two timer ID for our Gstplayer:
[CPP]View Plaincopy
- timer_t Mseektimer;
- timer_t Mprepareasynctimer;
3. Define the Createtimer function, create a timer, set the timeout function
Timerid: input and output parameters
Func:timer Timeout function
[CPP]View Plaincopy
- void Createtimer (timer_t *timerid, timertimeout func)
- {
- struct sigevent sev;
- Sev.sigev_notify = Sigev_thread;
- Sev.sigev_signo = Sigrtmin;
- Sev.sigev_value.sival_ptr = GPlayer;
- Sev.sigev_notify_function = func;
- Sev.sigev_notify_attributes = NULL;
- / * Create timer * /
- if (timer_create (Clock_realtime, &sev, timerid) = =-1)
- {
- ERR ("timer_create, error");
- return;
- }
- if (*timerid = =-1)
- ERR ("timer_create error, ID is-1");
- return;
- }
4. SetTimer function, call the Linux timer_settime, if not to time out, reset the previous timer
If it is already time out, you have to call Createtimer to generate a valid timer ID before calling SetTimer to start the timer.
-Here, set the interval parameter to 0 and specify that my timer does not work in loop mode.
-TIMEMSEC is the input parameter that specifies the time of day out in milliseconds.
[CPP]View Plaincopy
- void SetTimer (timer_t *timerid, int timemsec)
- {
- struct Itimerspec its;
- / * Start the timer * /
- Its.it_value.tv_sec = timemsec/1000;
- its.it_value.tv_nsec = (timemsec% 1000) * 1000000;
- its.it_interval.tv_sec = 0;
- its.it_interval.tv_nsec = 0;
- if (timer_settime (*timerid, 0, &its, NULL) = =-1)
- {
- ERR ("Timer_settime error");
- }
- DEBUG ("Call Timer_settime reset timer is done.");
- return;
- }
The Seektimertimeout function, after time out, destroys the timer created before the Createtimer is destroyed and the work to do after time is completed
[CPP]View Plaincopy
- void Seektimertimeout (Union sigval SIG)
- {
- Gstplayer *player = (gstplayerplayer*) sig.sival_ptr;
- if (player->mseektimer! =-1)
- {
- DEBUG ("timeout, delete Timer:id =%d",
- Player->mseektimer);
- Timer_delete (Player->mseektimer);
- Player->mseektimer =-1;
- }
- // ... Work to do after time is completed
- }
The time out function of the prepareasynctimeout:
[CPP]View Plaincopy
- void Prepareasynctimeout (Union sigval SIG)
- {
- Gststatechangereturn State_return;
- Gstplayer *player = (gstplayerplayer*) sig.sival_ptr;
- if (player->mprepareasynctimer! =-1)
- {
- DEBUG ("timeout, delete Timer:id =%d",
- Player->mprepareasynctimer);
- Timer_delete (Player->mprepareasynctimer);
- Player->mprepareasynctimer =-1;
- }
- // ... Work to do after time is completed
- }
Call One:
[CPP]View Plaincopy
- Create timer, set Prepareasynctimeout
- Start Timer,timeout time is 500ms
- Createtimer (&mprepareasynctimer, prepareasynctimeout);
- SetTimer (&mprepareasynctimer,/*ms*/);
Call Two:
[CPP]View Plaincopy
- Create a timer and set the timeout callback function.
- //Create timer
- if (Mseektimer = =-1)
- {
- Createtimer (&mseektimer, seektimertimeout);
- }
- Determine if the Mseektimer is valid, valid, and calculate the remaining time to timeout, if not to timeout, reset timer,
- Start a new timing.
- //If timer exist and not expire, reset timer.
- if (mseektimer! =-1)
- {
- Gulong remaining = 0; //us
- struct Itimerspec its;
- Timer_gettime (Mseektimer, &its);
- Remaining = Its.it_value.tv_sec * 1000000
- its.it_value.tv_nsec/1000;
- DEBUG ("--remaining time =%lu us", remaining);
- if ((/*ms*/* 1000-remaining) > 0)
- {
- SetTimer (&mseektimer,/*ms*/);
- DEBUG ("The new seek interval < 100ms, return");
- Mseekcount = 1;
- return TRUE;
- }
- }
Reference:
http://blog.163.com/zheng_he_xiang/blog/static/18650532620116311020390/
http://blog.csdn.net/leo9150285/article/details/8271910
"Go" Linux Posix timer use