What's the difference between yield() and sleep()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joecch
    New Member
    • May 2007
    • 5

    What's the difference between yield() and sleep()?

    What's the difference between yield() and sleep() in a multithreading program?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by joecch
    What's the difference between yield() and sleep() in a multithreading program?
    And what do the specs say about those methods?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by joecch
      What's the difference between yield() and sleep() in a multithreading program?
      Suppose we both want to enter a room; there's just one door though. When I
      yield, I'm just being polite so you can enter the room first. When I fall asleep
      you most certainly can enter the room first before I wake up.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Suppose we both want to enter a room; there's just one door though. When I
        yield, I'm just being polite so you can enter the room first. When I fall asleep
        you most certainly can enter the room first before I wake up.

        kind regards,

        Jos
        Notice how telling Jos to sleep will make him sleep whether you want to enter the room or not but when he yields he doesn't enter only if you want to get into the room as well.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Notice how telling Jos to sleep will make him sleep whether you want to enter the room or not but when he yields he doesn't enter only if you want to get into the room as well.
          So concluding: yielding is asking all other threads (if present) if they want to do
          something; if no other threads want to do something, I simply continue; if another
          thread ayes my question, I pass the processor to the other thread. Note that it
          doesn't matter whether or not something synchronized needs to happen; that
          scenario still works as always.

          kind regards,

          Jos

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            Originally posted by r035198x
            Notice how telling Jos to sleep will make him sleep whether you want to enter the room or not but when he yields he doesn't enter only if you want to get into the room as well.
            The question does Jos sleep???

            Sorry Jos...It seem you are alway on this board.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by nomad
              The question does Jos sleep???

              Sorry Jos...It seem you are alway on this board.
              Sleeps on the board ?

              Comment

              • sandeepsandeep
                New Member
                • Dec 2006
                • 50

                #8
                Suppose we are use sleep and set the 100 second to set sleep .so this thread wait 100 second to again start and in this time other thread is running when 100 second complete this thread is return and run.

                But in Yield same as sleep but difference is when 100 second is set but no other threads is not Queue in this time then this thread will not wait and again start .

                So sleep must be wait to set to specified time however others thread is queue or not but Yield is not

                Comment

                • nomad
                  Recognized Expert Contributor
                  • Mar 2007
                  • 664

                  #9
                  Originally posted by r035198x
                  Sleeps on the board ?
                  no sleep in general...

                  Comment

                  • deddla
                    New Member
                    • Oct 2010
                    • 1

                    #10
                    We call the yeild() method it will give chance to the other thread which is in runnable state .. and our thread will also go to the runnable state...
                    so that the Cpu Time sheduler may choose another thread to run
                    or it may choose our thread too...
                    there is no guarantee...

                    When we call sleep() method it will go for waiting state
                    but not to the runnable state so that the Cpu sheduler will pick up another thread from the runnable pool...
                    and our sleeping thread wakes up after certain amount of time and enter into the runnable state(waiting for cup time)....

                    Comment

                    Working...