免费注册 查看新帖 |

Chinaunix

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

pthread_delay_np [复制链接]

weizzil_chinaun 该用户已被删除
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-07-28 11:31 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
2 [报告]
发表于 2005-07-28 12:21 |只看该作者

pthread_delay_np

pthread_delay_np Subroutine
Purpose

Causes a thread to wait for a specified period.
Library

Threads Library (libpthreads.a)
Syntax

#include <pthread.h>;

int pthread_delay_np (interval)
struct timespec *interval;
Description

The pthread_delay_np subroutine causes the calling thread to delay execution for a specified period of elapsed wall clock time. The period of time the thread waits is at least as long as the number of seconds and nanoseconds specified in the interval parameter.

注意这里,看一下是不是没有将pthread.h放在源文件的首个头文件位置.
Notes:
The pthread.h header file must be the first included file of each source file using the threads library. Otherwise, the -D_THREAD_SAFE compilation flag should be used, or the cc_r compiler used. In this case, the flag is automatically set.
The pthread_delay_np subroutine is not portable.
Parameters
interval        Points to the time structure specifying the wait period.       
Return Values

Upon successful completion, 0 is returned. Otherwise, an error code is returned.
Error Codes

The pthread_delay_np subroutine is unsuccessful if the following is true:
EINVAL        The interval parameter is not valid.       
Implementation Specifics

This subroutine is part of the Base Operating System (BOS) Runtime.

This subroutine is not POSIX compliant and is provided only for compatibility with DCE threads. It should not be used when writing new applications.

论坛徽章:
0
3 [报告]
发表于 2005-07-28 13:39 |只看该作者

pthread_delay_np

linux下是没有pthread_delay_np,在Solaries下才有.
可以试一下下面一段代码进行线程延时
Not sure is sleep(3) is threadsafe. However, there is a more appropriate
and portable way to do this in threads by using a condition.
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t
*mutex, const struct timespec *abstime);


Yes, you need to set a mutex.
- --- code frag --
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;


pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
struct timespec ts;
int rv;
        ts.tv_sec = 0;
        ts.tv.nsec = 500000; /* 500,000 nanoseconds = 500 ms */


        pthread_mutex_lock(&mymutex);
        rv = pthread_cond_timedwait(&mycond, &mymutex, &ts);
        switch(rv) {
        case ETIMEDOUT:
                /* Handle timeout */
        case EINTR:
                /* Interupted by signal *.
        case EBUSY:
        default:
                /* Handle errors */
        case 0:
                /* condition received a condition signal */
        }
        pthread_mutex_unlock(&mymutex);
weizzil_chinaun 该用户已被删除
4 [报告]
发表于 2005-07-28 14:17 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2005-07-28 14:19 |只看该作者

pthread_delay_np

HP-UX下也没有,
其实可以用sleep或者usleep呀

论坛徽章:
0
6 [报告]
发表于 2005-07-29 13:56 |只看该作者

pthread_delay_np

用条件变量pthread_cond_timedwait比较保险,能保证是当前这个线程处于休眠状态,如果用sleep或usleep,可能使这个线程所处的进程休眠。
但我做过试验发现pthread_cond_timedwait和sleep或usleep的效果差不多,不过是在单cpu的环境中。如果是在多cpu的环境中,最好还是做个试验比较一下。
SirFang 该用户已被删除
7 [报告]
发表于 2005-07-29 18:41 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2005-10-27 16:02 |只看该作者

pthread_delay_np

我也是这个问题,谢谢了

论坛徽章:
0
9 [报告]
发表于 2005-10-27 20:43 |只看该作者

pthread_delay_np

原帖由 "lossve" 发表:
用条件变量pthread_cond_timedwait比较保险,能保证是当前这个线程处于休眠状态,如果用sleep或usleep,可能使这个线程所处的进程休眠。
但我做过试验发现pthread_cond_timedwait和sleep或usleep的效果差不多,不过..........

usleep 没有问题. 试验过.

论坛徽章:
0
10 [报告]
发表于 2007-11-19 14:33 |只看该作者
这个函数只在POSIX Threads for Window里面有,在其他的平台上是没有实现的。
函数sleep、usleep以及nanosleep都是阻塞当前的进程,那么属于该进程的所有的线程都将被阻塞。
在Linux上之所以 sleep这些函数阻塞的是当前线程而不阻塞当前进程,是因为在linux上是用进程
模拟线程的。
你到Solaris或者HU-UX上去跑一下你的程序就知道了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP