免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2499 | 回复: 4
打印 上一主题 下一主题

计算延时不用考虑 时钟调整和休眠? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-16 13:26 |只看该作者 |倒序浏览
误人子弟啊,这些所谓的权威。。。
都是狗屁代码

sun的
http://docs.sun.com/app/docs/doc ... pai?l=zh&a=view

  1. pthread_mutex_lock(&m);
  2. to.tv_sec = time(NULL) + TIMEOUT;
  3. to.tv_nsec = 0;
  4. while (cond == FALSE) {
  5.     err = pthread_cond_timedwait(&c, &m, &to);
  6.     if (err == ETIMEDOUT) {
  7.         /* timeout, do something */
  8.         break;
  9.     }
  10. }
  11. pthread_mutex_unlock(&m);
复制代码


好像是man里的
http://www.opengroup.org/onlinep ... read_cond_wait.html

  1. (void) pthread_mutex_lock(&t.mn);
  2.         t.waiters++;
  3.     clock_gettime(CLOCK_REALTIME, &ts);
  4.     ts.tv_sec += 5;
  5.     rc = 0;
  6.     while (! mypredicate(&t) && rc == 0)
  7.         rc = pthread_cond_timedwait(&t.cond, &t.mn, &ts);
  8.     t.waiters--;
  9.     if (rc == 0) setmystate(&t);
  10. (void) pthread_mutex_unlock(&t.mn);
复制代码

论坛徽章:
0
2 [报告]
发表于 2009-02-16 13:43 |只看该作者
再看ACE的,
WaitForSingleObject明明提供了延时时间,
ACE却要改用绝对时间,再和 gettimeofday 的结果相减一下,
如果恰好减之前调整下时间,又完蛋了。
简直自虐呀


  1. int
  2. ACE_OS::cond_timedwait (ACE_cond_t *cv,
  3.                         ACE_mutex_t *external_mutex,
  4.                         ACE_Time_Value *timeout)
  5. {
  6.   ACE_OS_TRACE ("ACE_OS::cond_timedwait");
  7. # if defined (ACE_HAS_THREADS)
  8.   // Handle the easy case first.
  9.   if (timeout == 0)
  10.     return ACE_OS::cond_wait (cv, external_mutex);
  11. #   if defined (ACE_HAS_WTHREADS) || defined (VXWORKS) || defined (ACE_PSOS)

  12.   // Prevent race conditions on the <waiters_> count.
  13.   ACE_OS::thread_mutex_lock (&cv->waiters_lock_);
  14.   cv->waiters_++;
  15.   ACE_OS::thread_mutex_unlock (&cv->waiters_lock_);

  16.   int result = 0;
  17.   ACE_Errno_Guard error (errno, 0);
  18.   int msec_timeout;

  19.   if (timeout->sec () == 0 && timeout->usec () == 0)
  20.     msec_timeout = 0; // Do a "poll."
  21.   else
  22.     {
  23.       // Note that we must convert between absolute time (which is
  24.       // passed as a parameter) and relative time (which is what
  25.       // WaitForSingleObjects() expects).
  26.       ACE_Time_Value relative_time (*timeout - ACE_OS::gettimeofday ());

  27.       // Watchout for situations where a context switch has caused the
  28.       // current time to be > the timeout.
  29.       if (relative_time < ACE_Time_Value::zero)
  30.         msec_timeout = 0;
  31.       else
  32.         msec_timeout = relative_time.msec ();
  33.     }

  34. #     if defined (ACE_HAS_SIGNAL_OBJECT_AND_WAIT)
  35.   if (external_mutex->type_ == USYNC_PROCESS)
  36.     // This call will automatically release the mutex and wait on the
  37.     // semaphore.
  38.     result = ::SignalObjectAndWait (external_mutex->proc_mutex_,
  39.                                     cv->sema_,
  40.                                     msec_timeout,
  41.                                     FALSE);

复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2009-02-16 13:50 |只看该作者
这个错误我也犯过,
导致计算出来的带宽有节律性的异常数据(毛刺)

论坛徽章:
0
4 [报告]
发表于 2009-02-16 13:58 |只看该作者

回复 #3 flw 的帖子

可能用 times() 或 GetTickCount() 的返回值比较好。
后来怎么解决的?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2009-02-16 14:04 |只看该作者
原帖由 太平绅士 于 2009-2-16 13:58 发表
可能用 times() 或 GetTickCount() 的返回值比较好。
后来怎么解决的?

后来用的 /proc/uptime,精度比较低,仅到了 10ms 级,但是经过计算,认为误差不影响业务,所以就暂时这样了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP