Yield in a Cocoa while loop

Oldtimer
Posts: 832
Joined: 2002.09
Post: #1
I'm trying to rework the event handling mechanism in my engine, with the goal of getting it to stop hog 100% CPU when it can. I realize that I should be on a timer, but due to the way its Win port works and the general structure of the engine, this isn't likely to happen anytime now.

I need to enter a function and not return until the application is ready to quit. Right now I do this by trapping myself in a while loop calling

Code:
[[NSRunLoop currentRunLoop] runUntilDate: distantPast];

event = [NSApp    nextEventMatchingMask: eventMask
                untilDate: distantPast
                inMode: NSEventTrackingRunLoopMode
                dequeue: YES];

Unfortunately, this burns through cycles like nothing else since the main thread never yields to other apps. One simple solution would be to add an explicit yield for a fraction of a second after each rendered frame, but I haven't found a way to make Cocoa suspend itself. So, my primary question is if there's a simple, overlooked way to make the run loop run and yield? Would calling runUntilDate: "now + 1/60th second" actually suspend the thread for a while?

Another solution would be to lock myself into a new run loop with a timer that calls my update function at the desired interval - does this sound possible and desirable?

Thanks, as always.
Quote this message in a reply
Post Reply 


Messages In This Thread
Yield in a Cocoa while loop - Fenris - Dec 27, 2007 08:48 AM
Yield in a Cocoa while loop - ThemsAllTook - Dec 27, 2007, 10:27 AM
Yield in a Cocoa while loop - mattz - Dec 27, 2007, 11:38 AM
Yield in a Cocoa while loop - SethWillits - Dec 27, 2007, 01:21 PM
Yield in a Cocoa while loop - OneSadCookie - Dec 27, 2007, 01:55 PM
Yield in a Cocoa while loop - Fenris - Dec 27, 2007, 02:41 PM
Yield in a Cocoa while loop - OneSadCookie - Dec 27, 2007, 10:09 PM
Yield in a Cocoa while loop - Fenris - Dec 28, 2007, 02:00 AM
Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Proper game update loop in Cocoa? LoneIgadzra 13 5,704 May 30, 2008 05:49 AM
Last Post: ThemsAllTook
  Cocoa Event Loop/NSTimer revisited Fenris 6 4,582 Oct 29, 2005 11:27 PM
Last Post: maaaaark
  Game Loop in Cocoa robmcq 9 5,347 Sep 27, 2005 10:49 AM
Last Post: akb825