分享

钱超的空间: “获得人生中的成功需要的专注与坚持不懈多过天才与机会”

 jijo 2008-08-13


“获得人生中的成功需要的专注与坚持不懈多过天才与机会”

“获得人生中的成功需要的专注与坚持不懈多过天才与机会”
今天偶尔看到了这句,拿出来鞭策一下自己最近非常的懈怠的状态。
 
最近在开发一个小的feature,就是在UI上面显示一个x/y的进度提示,当然,因为小所以我一个人负责。压力不小。
由于数据包的发送存在无法精确估计打包粒度的问题,所以只好使用了模拟的方式实现,因此需要弄一个定时器(Timer)来实现包与包之间数据的跳跃。
 
从网上找到timer得实现部分如下。当然,别忘记了加入linux的几个头文件。
 
实现一:
创建和使用POSIX timer的C代码。创建timer有两个步骤:指定timer到期时发送的信号,然后创建设置timer本身。例子中使用了最
高优先级的实时信号 SIGRTMIN来异步调用timer处理例程。必须指定timer的两个值:初始到期时间it_value和频率tv_sec。结构 itimersepc
允许纳秒级的时间规格,不过真正的精度和系统有关。POSIX调用clock_getres()可以用来确定真正的时间精度,通常是 10ms或者1ms。
 
#include
#include
void timer_create(int num_secs, int num_nsecs)
{
struct sigaction sa;
struct sigevent sig_spec;
sigset_t allsigs;
struct itimerspec tmr_setting;
timer_t timer_h;
/* setup signal to respond to timer */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timer_intr;
if (sigaction(SIGRTMIN, &sa, NULL) < 0)
perror(3sigaction2);
sig_spec.sigev_notify = SIGEV_SIGNAL;
sig_spec.sigev_signo = SIGRTMIN;
/* create timer, which uses the REALTIME clock */
if (timer_create(CLOCK_REALTIME, &sig_spec, &timer_h) < 0)
perror(3timer create2);
/* set the initial expiration and frequency of timer */
tmr_setting.it_value.tv_sec = 1;
tmr_setting.it_value.tv_nsec = 0;
tmr_setting.it_interval.tv_sec = num_secs;
tmr_setting.it_interval.tv_sec = num_nsecs;
if ( timer_settime(timer_h, 0, &tmr_setting,NULL) < 0)
perror(3settimer2);
/* wait for signals */
sigemptyset(&allsigs);
while (1) {
sigsuspend(&allsigs);
}
}
/* routine that is called when timer expires */
void timer_intr(int sig, siginfo_t *extra, void *cruft)
{
/* perform periodic processing and then exit */
}
实现二:
///* ******mytimer.h****************
#ifndef _111_FILE_
  #define _111_FILE_
   #include <iostream>;
   #include <pthread.h>;
   #include <unistd.h>;
   #include <stdlib.h>;
   #include <signal.h>;
   #include <pthread.h>;
   #include <unistd.h>;
   #include <sys/stat.h>;
   #include <string.h>;
   #include <time.h>;
   #include <stdio.h>;
   #include <sys/time.h>;
  #include <fstream.h>;
typedef enum { S0_ok,S1_geta} alarm_record_status ;
 
  int SetTimer(int n, int nMode =0);
  void TimerRoutine(int signo, siginfo_t* info, void* context);
  
#endif
 
//////////////////////////////////////mytimer.cxx
#include "myapp.h"
#define SIGMYTIMER1 (SIGRTMAX)
using namespace std;
int main(int argc,char * argv[])
{
//
//start timer1
                  struct sigaction sysact ;
                  sigemptyset(&sysact.sa_mask);
                  sysact.sa_flags = SA_SIGINFO;
                  sysact.sa_sigaction = TimerRoutine1 ;
                  sigaction(SIGMYTIMER1, &sysact, NULL);
                 // nTimerFlag=1;
                  nTimer1ID=SetTimer(milliSecTimer1); //start timer1 as main timer:milliSecTimer1
                 
                 
  }
 
  //mode: 0  one time return operiod return
int SetTimer(int nElaspe, int nMode=1)
{
    struct sigevent evp;
    timer_t nTimerID;
  
    evp.sigev_notify = SIGEV_SIGNAL;
    evp.sigev_signo = SIGMYTIMER;
    evp.sigev_value.sival_ptr = &nTimerID;
    int nCreate = timer_create(CLOCK_REALTIME, &evp, &nTimerID);
  
    if (nCreate == 0) //success
    {
       struct itimerspec value;
       struct itimerspec ovalue;
     
       value.it_value.tv_sec = nElaspe / 1000;
       value.it_value.tv_nsec = (nElaspe % 1000) * (1000 * 1000);
     
       if (nMode == 1)
       {
         value.it_interval.tv_sec = value.it_value.tv_sec;
         value.it_interval.tv_nsec = value.it_value.tv_nsec;
       }
       else
       {
         value.it_interval.tv_sec = 0;
         value.it_interval.tv_nsec = 0;
       }
  
       if (timer_settime(nTimerID, 0, &value, &ovalue) == 0) //success
       {
         cout<<"Timer id:"<<nTimerID<<" nElaspe:"<<nElaspe<<endl;
       }
    }
    else
    {
    cout<<"create timer error"<<endl;
    }
    return nTimerID;
}
void TimerRoutine1(int signo, siginfo_t* info, void* context)
{
   if (signo != SIGMYTIMER)
   {
             return;
    }
    // do your job here
}
 
以上两个例子其实要实现的 以及实现的方式都是一样的,都贴出来不过是为了相互补充一下。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多