简体   繁体   English

float值in - struct itimerspec

[英]float value in - struct itimerspec

I have this problem: I have created a structure using the itimerspec structure. 我有这个问题:我使用itimerspec结构创建了一个结构。 The itimerspec structure has two fields: itimerspec结构有两个字段:

 struct timespec {
           time_t tv_sec;                /* Seconds */
           long   tv_nsec;               /* Nanoseconds */
       };

       struct itimerspec {
           struct timespec it_interval;  /* Timer interval */
           struct timespec it_value;     /* Initial expiration */
       };

So when i am entering: 所以当我进入时:

enter code here
     struct itimerspec its; // argument to timer_gettime
      /* Setting timer interval */

       its.it_interval.tv_sec=0; 
       its.it_interval.tv_nsec=1;

      /* Setting timer expiration */
       its.it_value.tv_sec=0.1;  // First expiry after 1 sec
       its.it_value.tv_nsec=0;

     On compilation:
       prototype1.cc:115: warning: converting to ‘__time_t’ from ‘double’

My problem is: as per the design, the user can enter the timer expiration in whole numbers (1, 2, 3 etc which is fine) but can also enter time like 0.1 secs, o.2 secs etc. But only in seconds. 我的问题是:根据设计,用户可以输入整数的定时器到期(1,2,3等等,这很好),但也可以输入时间,如0.1秒,o.2秒等。但只有几秒钟。

You can't assign a double to a long val. 您不能将double赋给long val。 If you need less than 1 sec you should express it in term of usec. 如果你需要不到1秒的时间,你应该用usec来表达它。

0.1 sec = 100000 usec 0.1秒= 100000 usec

Do: 做:

its.it_value.tv_sec=0;  // First expiry after 1 sec
its.it_value.tv_nsec= 0.1 * (usec in sec);

您将必须调整为秒和纳秒,例如0.1秒= 0秒和100,000毫秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM