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. Thanks Zsolt
Lukacs Zsolt wrote:
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.
Thanks
Zsolt
Need to look at the code a bit. I don't seem to have the same trouble when using pthreads.
Dear Zsolt, You want to use pthread_cont_timedwait(). It's described in the manual page pthread_cond(3). It's also described in the GNU Info pages for "libc", in the node "Condition Variables". --Steve Augart -- Steven Augart Jikes RVM Open Source Java Virtual Machine Project http://oss.software.ibm.com/jikesrvm Office: +1 914/784-6743 Lukacs Zsolt <zsolt.lukacs@siemens.com> 01/26/2004 01:22 PM To: SuSE Programming <suse-programming-e@suse.com> cc: Subject: [suse-programming-e] Thread sleep 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. Thanks Zsolt -- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Mon, 26 Jan 2004 10:22:14 -0800 Lukacs Zsolt <zsolt.lukacs@siemens.com> wrote:
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 <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux) iD8DBQFAFXUU+wA+1cUGHqkRAt5/AJ4pNBL9sjhl9n/G1ZGhMYqZrkcecACgh93/ 7nYDadstbSMF5QMqjO6Z+pk= =Gdvv -----END PGP SIGNATURE-----
Thanks to all. I think that's what I have been looking for. :) Zsolt Jerry Feldman wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Mon, 26 Jan 2004 10:22:14 -0800 Lukacs Zsolt <zsolt.lukacs@siemens.com> wrote:
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 <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQFAFXUU+wA+1cUGHqkRAt5/AJ4pNBL9sjhl9n/G1ZGhMYqZrkcecACgh93/ 7nYDadstbSMF5QMqjO6Z+pk= =Gdvv -----END PGP SIGNATURE-----
participants (4)
-
expatriate
-
Jerry Feldman
-
Lukacs Zsolt
-
Steven Augart