-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Mon, 26 Jan 2004 10:22:14 -0800
Lukacs Zsolt
Hi,
I have created a process which cretes some threads. I would like to sleep one thread (let's say for 500 ms) while the other threads are still working. Does anybody know how to do that? I have already tried to use the sleep() function but it seems that it the whole process goes to sleep not just olny one of the threads owned
by the process.
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);
- --
Jerry Feldman