To sleep, or not to sleep?

Started by elias4444, August 24, 2005, 14:31:06

Previous topic - Next topic

elias4444

Out of curiosity, in our rendering loops, should we specify a thread.sleep() or thread.yield()? Personally, I've done both, as well as using nothing at all. Currently, I'm not calling either one in my games (figuring that gives them maximum performance), but I'm wondering if that's a poor choice. Any advice?
=-=-=-=-=-======-=-=-=-=-=-
http://www.tommytwisters.com

darkprophet

Thread.sleep(1); will give you the sleep according to the OS's granuality. In windows, thats about 15 - 20 millis which might not be acceptable. Thread.yield() just moves away and gives other threads a chance to do some work.

Depending on the type of work the other threads have to do, the FPS can jump up and down in yield, however, the end result is smooth as the OS doesn't need to push the game thread manually to make room for other threads.

Personally, i would yield...But thats just my opinion. I know Cas sleeps instead...

DP

princec


CaseyB

Aren't yield() and sleep(0) the same?

princec


K.I.L.E.R

Here is a little test:

code:
double tN;
		
		tN = System.nanoTime();
		
			Thread.yield();/*or tested equivalent*/
		
		System.out.println(System.nanoTime() - tN);


Each test was run for as long as needed until results were very similar to one another.
The median result out of many variations was taken on each test.

My OS is Windows XP Home.

m.wait(1):
1810285.0 / 1e6 = 1.810285ms

m.wait(0, 1):
1477562.0 / 1e6 = 1.477562

Thread.yield():
106438.0 / 1e6 = 0.106438ms

Thread.sleep(1):
2116470.0 / 1e6 = 2.11647ms (far from the 16ms you guys are talking about, yes I ran this a lot of times).

Thread.sleep(0, 1):
2515124.0 / 1e6 = 2.515124ms

Thread.sleep(0):
897600.0 / 1e6 = 0.8976ms

I made a thread about this on JG.org.[/code]