"Go" Linux Posix timer use

Source: Internet
Author: User
Tags posix

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
  1. Timer_create (2) Linux Programmer ' s Manual
  2. NAME
  3. Timer_create-create a POSIX per-process timer
  4. Synopsis
  5. #include <signal.h>
  6. #include <time.h>
  7. int timer_create (clockid_t clockid, struct sigevent *sevp,
  8. timer_t *timerid);
  9. int Timer_settime (timer_t timerid, int flags,
  10. const struct itimerspec *new_value,
  11. struct Itimerspec * old_value);
  12. 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
    1. typedef VOID (*timertimeout) (Union sigval SIG);


2. Define a two timer ID for our Gstplayer:

[CPP]View Plaincopy
    1. timer_t Mseektimer;
    2. 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
  1. void Createtimer (timer_t *timerid, timertimeout func)
  2. {
  3. struct sigevent sev;
  4. Sev.sigev_notify = Sigev_thread;
  5. Sev.sigev_signo = Sigrtmin;
  6. Sev.sigev_value.sival_ptr = GPlayer;
  7. Sev.sigev_notify_function = func;
  8. Sev.sigev_notify_attributes = NULL;
  9. / * Create timer * /
  10. if (timer_create (Clock_realtime, &sev, timerid) = =-1)
  11. {
  12. ERR ("timer_create, error");
  13. return;
  14. }
  15. if (*timerid = =-1)
  16. ERR ("timer_create error, ID is-1");
  17. return;
  18. }

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
  1. void SetTimer (timer_t *timerid, int timemsec)
  2. {
  3. struct Itimerspec its;
  4. / * Start the timer * /
  5. Its.it_value.tv_sec = timemsec/1000;
  6. its.it_value.tv_nsec = (timemsec% 1000) * 1000000;
  7. its.it_interval.tv_sec = 0;
  8. its.it_interval.tv_nsec = 0;
  9. if (timer_settime (*timerid, 0, &its, NULL) = =-1)
  10. {
  11. ERR ("Timer_settime error");
  12. }
  13. DEBUG ("Call Timer_settime reset timer is done.");
  14. return;
  15. }

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
  1. void Seektimertimeout (Union sigval SIG)
  2. {
  3. Gstplayer *player = (gstplayerplayer*) sig.sival_ptr;
  4. if (player->mseektimer! =-1)
  5. {
  6. DEBUG ("timeout, delete Timer:id =%d",
  7. Player->mseektimer);
  8. Timer_delete (Player->mseektimer);
  9. Player->mseektimer =-1;
  10. }
  11. // ... Work to do after time is completed
  12. }


The time out function of the prepareasynctimeout:

[CPP]View Plaincopy
  1. void Prepareasynctimeout (Union sigval SIG)
  2. {
  3. Gststatechangereturn State_return;
  4. Gstplayer *player = (gstplayerplayer*) sig.sival_ptr;
  5. if (player->mprepareasynctimer! =-1)
  6. {
  7. DEBUG ("timeout, delete Timer:id =%d",
  8. Player->mprepareasynctimer);
  9. Timer_delete (Player->mprepareasynctimer);
  10. Player->mprepareasynctimer =-1;
  11. }
  12. // ... Work to do after time is completed
  13. }


Call One:

[CPP]View Plaincopy
    1. Create timer, set Prepareasynctimeout
    2. Start Timer,timeout time is 500ms
    3. Createtimer (&mprepareasynctimer, prepareasynctimeout);
    4. SetTimer (&mprepareasynctimer,/*ms*/);


Call Two:

[CPP]View Plaincopy
  1. Create a timer and set the timeout callback function.
  2. //Create timer
  3. if (Mseektimer = =-1)
  4. {
  5. Createtimer (&mseektimer, seektimertimeout);
  6. }
  7. Determine if the Mseektimer is valid, valid, and calculate the remaining time to timeout, if not to timeout, reset timer,
  8. Start a new timing.
  9. //If timer exist and not expire, reset timer.
  10. if (mseektimer! =-1)
  11. {
  12. Gulong remaining = 0; //us
  13. struct Itimerspec its;
  14. Timer_gettime (Mseektimer, &its);
  15. Remaining = Its.it_value.tv_sec * 1000000
  16. its.it_value.tv_nsec/1000;
  17. DEBUG ("--remaining time =%lu us", remaining);
  18. if ((/*ms*/* 1000-remaining) > 0)
  19. {
  20. SetTimer (&mseektimer,/*ms*/);
  21. DEBUG ("The new seek interval < 100ms, return");
  22. Mseekcount = 1;
  23. return TRUE;
  24. }
  25. }

Reference:

http://blog.163.com/zheng_he_xiang/blog/static/18650532620116311020390/
http://blog.csdn.net/leo9150285/article/details/8271910

"Go" Linux Posix timer use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.