UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » delay just one specific thread for n seconds

#1 2007-11-19 12:37 PM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: delay just one specific thread for n seconds

hello, i must delay a thread for a couple of seconds, i was using
the old one "sleep(seconds)" but it's giving me problems because it sleeps or "hangs" the entire process, all the program, and other threads that must be in contact with the client (mine is the server) stop working. I looked at the sleep man page and it says that "sleep the process".

is there any way to sleep a thread, but just that thread (not the others) ?

thanks!!!

Offline

#2 2007-11-19 12:51 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: delay just one specific thread for n seconds

Try nanosleep().

What kernel and pthread implementations are you using? I think you're using an old pthreads implementation, try upgrading it.

Offline

#3 2007-11-19 12:58 PM

darthsidious
Member
Registered: 2007-11-18
Posts: 14

Re: delay just one specific thread for n seconds

i'm using pthread.h with pthread_create, pthread_join, pthread_exit

i'new with this, it's something for college and it's driving me mad.

does nanosleep only affects the current thread?? i don't want other threads in that process to sleep or stop working.

i've seen on some webs a function called pthread_cond_timedwait but you have to set a condition to wait. Is there any way to use this function just for waiting? i.e without any real condition.

thanks!!!

ps. what about pthread_delay_np?

Offline

#4 2007-11-19 01:53 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: delay just one specific thread for n seconds

nanosleep() should be fine...  Another alternative is select() with no fd_sets...  Either
should be thread-safe...  sleep() is not guaranteed to be thread-safe...  (But, on
modern versions of glibc, I think it actually is anyway...)  However, even if it's not
thread-safe, I find it strange that it'd actually cause all threads to actually go to sleep,
too...  At worst, I'd think it'd cause signal-related problems...  But, I don't get why it'd
be really causing all threads to really sleep...  What system is this being run on?  Is
it maybe one with some kind of strange user-space threads implementation, rather
than a kernel-based one?  Because, that might do it...  In that case, then yeah, you
could do something like pthread_cond_timedwait() on a condition that is never signalled,
and that should function as a decent sleep as well...

Offline

#5 2007-11-19 04:59 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: delay just one specific thread for n seconds

Another possibility:  he's calling sleep() while the thread is holding a lock/mutex of some sort, and all the other threads try to lock that mutex every so often and therefore fall asleep waiting for the first thread to wake up and unlock it...

Offline

  • Index
  • » Threads
  • » delay just one specific thread for n seconds

Board footer

Powered by FluxBB