473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

5
What's the difference between yield() and sleep() in a multithreading program?
Jun 1 '07 #1
9 10609
r035198x
13,262 8TB
What's the difference between yield() and sleep() in a multithreading program?
And what do the specs say about those methods?
Jun 1 '07 #2
JosAH
11,448 Expert 8TB
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
Jun 1 '07 #3
r035198x
13,262 8TB
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.
Jun 1 '07 #4
JosAH
11,448 Expert 8TB
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
Jun 1 '07 #5
nomad
664 Expert 512MB
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.
Jun 1 '07 #6
r035198x
13,262 8TB
The question does Jos sleep???

Sorry Jos...It seem you are alway on this board.
Sleeps on the board ?
Jun 2 '07 #7
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
Jun 2 '07 #8
nomad
664 Expert 512MB
Sleeps on the board ?
no sleep in general...
Jun 2 '07 #9
deddla
1
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)....
Oct 7 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
7
by: sachin_mzn | last post by:
Hi, It may be a silly question but I want to know the difference between #define macro and inline functions Is there any performance issue related to it. -Sachin
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
5
by: bughunter | last post by:
Hi, Consider this code: ---- Monitor.Pulse(oLock); Monitor.Exit(oLock); ---- If a thread was waiting on oLock then will the current thread
2
by: yyhhjj | last post by:
I created a test program to implement an iterator. First, I used 'yield break' in the iterator, it worked normally. Then, I simply used 'break' in the places of 'yield break', it still worked...
4
by: peterbe | last post by:
This works exactly as you would expect:: from time import sleep def foo(on='ABC'): for e in list(on): sleep(1) yield e When I run this on the command line It takes about 3 seconds to...
5
by: cjl | last post by:
Hi. I am trying to screen scrape some stock data from yahoo, so I am trying to use urllib2 to retrieve the html and beautiful soup for the parsing. Maybe (most likely) I am doing something...
11
by: beginner | last post by:
Hi, If I have a number n and want to generate a list based on like the following: def f(n): l= while n>0: l.append(n%26) n /=26
0
by: DR | last post by:
is there any speed difference between a TVF CLR that returns an array vs yield as each result is ready? e.g. does returning the array all at once cause sql server to allocate more efficiently?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.