免费注册 查看新帖 |

Chinaunix

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

请问怎样让线程delay or sleep? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-03-01 10:18 |只看该作者 |倒序浏览
以前为了让某个线程单独延时都是用的usleep/nanusleep,有人说这会让整个进程都sleep,是真的吗?man里也没说明,还有pthread_delay_np  好象linux也不支持吧


NAME
       getitimer, setitimer ? get or set value of an interval timer

SYNOPSIS
       #include <sys/time.h>;

       int getitimer(int which, struct itimerval *value);
       int  setitimer(int which, const struct itimerval *value, struct itimer?\
              val *ovalue);

DESCRIPTION
       The system provides each  process  with  three  interval  timers,  each
       decrementing in a distinct time domain.  When any timer expires, a sig?\
       nal is sent to the process, and the timer (potentially) restarts.

**************************************************************

NAME
       usleep ? suspend execution for microsecond intervals

SYNOPSIS
       #include <unistd.h>;

       void usleep(unsigned long usec);
       int usleep(unsigned long usec); /* SUSv2 */

DESCRIPTION
       The  usleep()  function  suspends  execution of the calling process for
       usec microseconds.  The sleep may be lengthened slightly by any  system
       activity or by the time spent processing the call.

**************************************************************

NAME
       nanosleep ? pause execution for a specified time

SYNOPSIS
       #include <time.h>;

       int nanosleep(const struct timespec *req, struct timespec *rem);

DESCRIPTION
       nanosleep  delays  the  execution  of the program for at least the time
       specified in *req.  The function can return earlier  if  a  signal  has
       been  delivered to the process. In this case, it returns ?1, sets errno
       to EINTR, and writes the remaining time into the structure  pointed  to
       by  rem unless rem is NULL.  The value of *rem can then be used to call
       nanosleep again and complete the specified pause.

论坛徽章:
0
2 [报告]
发表于 2004-03-01 12:35 |只看该作者

请问怎样让线程delay or sleep?

直接用select吧,三个掩码都设为NULL,纯粹做为定时器用

论坛徽章:
0
3 [报告]
发表于 2004-03-01 13:32 |只看该作者

请问怎样让线程delay or sleep?

sleep usleep可以的。

论坛徽章:
0
4 [报告]
发表于 2004-03-01 18:19 |只看该作者

请问怎样让线程delay or sleep?

主要是对 man 的说明有点疑惑。。。

论坛徽章:
0
5 [报告]
发表于 2004-03-02 09:03 |只看该作者

请问怎样让线程delay or sleep?

usleep函数最好不要用,在多线程环境中它的可移植性不好,例如在solaris5.8下它就不是多线程安全的,会导致线程无法醒来。而sleep函数的分辨率太低,很多应用都不适合,所以还是用select最好,包装一下就可以作为一个方便的延时器了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP