CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    609

    threads and sleep

    I read in another thread that using sleep or usleep causes not only one thread to sleep, but the whole program. Is there an alternative way to make just the one thread its called from sleep instead of the whole program?
    http://www.uovalor.com :: Free UO Server

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: threads and sleep

    In general using sleep within a thread will only cause that thread to sleep and not the whole program.

    The exception to this is when the sleeping thread has obtained a lock and other thread(s) are waiting to obtain the lock - the end effect is that the waiting thread(s) are essentially sleeping too.

    The use of sleep should be avoided. Sometimes it is helpful to yield the remainder of a time slice by using a sleep(0), but usually times where sleep has been used can often be resolved by proper thread synchronization.

  3. #3
    Join Date
    Jul 2008
    Location
    Be'er Sheva, Israel
    Posts
    69

    Re: threads and sleep

    Quote Originally Posted by Arjay
    The use of sleep should be avoided. Sometimes it is helpful to yield the remainder of a time slice by using a sleep(0), but usually times where sleep has been used can often be resolved by proper thread synchronization.
    You don't even need sleep(0) for that, simply use sched_yield to achieve the same goal.
    --How often do you look at a man's shoes?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: threads and sleep

    Quote Originally Posted by ishaypeled
    You don't even need sleep(0) for that, simply use sched_yield to achieve the same goal.
    Sorry, I was referring to the Windows api.

  5. #5
    Join Date
    Oct 2008
    Location
    bhubaneswar
    Posts
    3

    Re: threads and sleep

    It depends....

    no thread should use sleep(n) in its critical section...which will reduce the performance.

    some times sleep() is used to avoid the situation of pooling which eats unnecessary CPU cycles.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: threads and sleep

    I was taught that sched_yield was only to be used by the threading implementation, and that *any* use of it by a client program was a bug.

    Makes sense to me----either you want to wait for a specific event, in which case a thread condition wait, mutex, or spinlock is appropriate, or else you want to make progress. Simply yielding control without specifying *why* you're doing so is pretty useless.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured