Skip to main content
14 events
when toggle format what by license comment
Jul 11, 2011 at 11:29 comment added Maik Semder @Roy I would say so too :) one should never expect the timing of sleep to be exactly, the number passed as parameter is more like a hint to the scheduler, the only assertation is, that is doesnt come back earlier. So yeah, best use 0 as param, that tells the scheduler, "get back as soon as possible", which is usually less than small fractions of milliseconds
Jul 11, 2011 at 10:37 comment added Roy T. @Maik Hmm, OK that last argument got me! You're right ofc the OS can transfer your process from running to ready anytime, so yes sleep(0) would be better :). So advice for the one who asked the question would be: use sleep(0) not sleep(15ms).
Jul 10, 2011 at 18:08 comment added Maik Semder Btw. I saw a lot of games using sleep(0), especially multi-threaded ones
Jul 10, 2011 at 18:06 comment added Maik Semder @Roy under normal conditions the sleep(0) will return in time. But of course, as you already said in your answer, one has to be prepared and deal with the situation that it doesnt and skip some parts. But this you have to do anyway, that's not an argument for adding unnecessary workload to the CPU. I totally agree with the rest of your answer, that's why I didn't downvote it. I just disagree to the part saying that it's better to have a busy wait blocking all threads of the app and the system, than a using a sleep(0) :)
Jul 10, 2011 at 18:06 comment added Maik Semder @Roy you can't guarantee that. You can't guarantee that with any solution, as we are a) not talking about real time operation systems and b) you have no control what the operation system chooses to do. The point is, when the system is under that much workload, that it wouldn't return in time, then you have much worse problems. But you can't solve that by adding an even heavier workload to the CPU. It's throwing oil into the fire.
Jul 10, 2011 at 17:32 comment added Roy T. @Maik Semder, but how do you guarantee that you return in 15ms or less. I mean the purpose of sleep here was using it to run in fixed-time step mode :). If you're not running in fixed time step than you don't need sleep (because you can use any of the algorithms described above). I've never seen a game do sleep(..) tbh. I wonder if I can find some resources on how XNA achieves fixed-time step.
Jul 10, 2011 at 14:46 comment added Maik Semder @Roy sleep(0) is the solution. It returns immediately if there is no other thread that wants to run (Sleep WinAPI) and gives other threads the chance to run. If the other thread will not give the main thread the chance to run in return, then you have a threading problem, but blocking everything else by not calling sleep in the first place makes it even worse and is hardly a solution. The key is to call sleep(0) and test the elapsed time until you hit your target fixed frame rate, so you dont waste 100 % CPU just for waiting.
Jul 10, 2011 at 14:16 comment added Roy T. @Maik Semder: do you have a solution for sleep(x) not being accurate? After the sleep interval has passed, the thread is ready to run. But a ready thread is not guaranteed to run immediately. That's up to the scheduler. When you're using two threads there are other solutions, for that see this excellent article: altdevblogaday.com/2011/07/03/threading-and-your-game-loop
Jul 10, 2011 at 14:09 comment added Maik Semder @Oskenso However, it is a problem if you use more than 1 thread, then the main thread will not let the others run as much as they could, wasting a lot of computational power in the while loop, you really should consider a sleep
Jul 10, 2011 at 7:26 comment added Roy T. It will consume as much cycles as the scheduler allows it to get, but that's not really an issue since you can't really do a lot of other things while you're playing a game :).
Jul 9, 2011 at 20:39 vote accept Osky
Jul 9, 2011 at 20:39 vote accept Osky
Jul 9, 2011 at 20:39
Jul 9, 2011 at 19:48 comment added Osky So my engine will always consume 100% and there's nothing I can really do about it?
Jul 9, 2011 at 10:23 history answered Roy T. CC BY-SA 3.0