LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: elias4444 on August 24, 2005, 14:31:06

Title: To sleep, or not to sleep?
Post by: elias4444 on August 24, 2005, 14:31:06
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?
Title: To sleep, or not to sleep?
Post by: darkprophet on August 24, 2005, 15:24:05
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
Title: To sleep, or not to sleep?
Post by: princec on August 24, 2005, 17:16:45
I do a sleep(0).

Cas :)
Title: To sleep, or not to sleep?
Post by: CaseyB on August 24, 2005, 17:46:50
Aren't yield() and sleep(0) the same?
Title: To sleep, or not to sleep?
Post by: princec on August 24, 2005, 18:16:34
Apparently not.

Cas :)
Title: To sleep, or not to sleep?
Post by: K.I.L.E.R on October 01, 2005, 13:01:57
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]