pthread_delay_np
pthread_delay_np SubroutinePurpose
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.
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);
pthread_delay_np
HP-UX下也没有,其实可以用sleep或者usleep呀
pthread_delay_np
用条件变量pthread_cond_timedwait比较保险,能保证是当前这个线程处于休眠状态,如果用sleep或usleep,可能使这个线程所处的进程休眠。但我做过试验发现pthread_cond_timedwait和sleep或usleep的效果差不多,不过是在单cpu的环境中。如果是在多cpu的环境中,最好还是做个试验比较一下。
pthread_delay_np
我也是这个问题,谢谢了pthread_delay_np
原帖由 "lossve" 发表:用条件变量pthread_cond_timedwait比较保险,能保证是当前这个线程处于休眠状态,如果用sleep或usleep,可能使这个线程所处的进程休眠。
但我做过试验发现pthread_cond_timedwait和sleep或usleep的效果差不多,不过..........
usleep 没有问题. 试验过. 这个函数只在POSIX Threads for Window里面有,在其他的平台上是没有实现的。
函数sleep、usleep以及nanosleep都是阻塞当前的进程,那么属于该进程的所有的线程都将被阻塞。
在Linux上之所以 sleep这些函数阻塞的是当前线程而不阻塞当前进程,是因为在linux上是用进程
模拟线程的。
你到Solaris或者HU-UX上去跑一下你的程序就知道了!
页:
[1]
2