Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:28:22+00:00 2026-06-09T23:28:22+00:00

I’m trying to better understand run loops as they apply to Mac applications (

  • 0

I’m trying to better understand run loops as they apply to Mac applications (NSRunLoop), but this could also be a more general question. The NSRunLoop documentation says:

…your code provides the while or for loop that drives the run loop. Within your loop, you use a run loop object to “run” the event-processing code that receives events and calls the installed handlers.

The docs have a code example like this:

BOOL shouldKeepRunning = YES;
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);

So the code keeps calling the run loop until it’s decided that it should terminate. The -runMode:beforeDate: method “Runs the loop once, blocking for input in the specified mode until a given date.” And there is also a -run method, which “Puts the receiver into a permanent loop, during which time it processes data from all attached input sources.”

How is it possible that calling the run loop repeatedly (or calling -run which, it sounds like, does this itself) doesn’t consume CPU? A Cocoa application can sit idly in the background while its main run loop runs, and it will consume zero (or nearly zero) CPU time.

And within -runMode:beforeDate:, how can the run loop block until input is received or timers fire without polling and consuming CPU?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:28:23+00:00Added an answer on June 9, 2026 at 11:28 pm

    This was a good excuse to poke into some Mac OS X internals! Luckily the relevant parts of Core Foundation are open source.

    The usual answer to “How can a program wait for X without taking up CPU?” is “The kernel did it.” In this case, running the run loop really just means telling the kernel what you’re waiting for, and then letting the kernel context-switch away. In this case, the run loop spends most of its time in mach_msg with the flag MACH_RCV_MSG, which really is just a system call into the kernel, which ends up scheduling some other thread to run. Eventually, something interesting happens, which means that a Mach message is sent to a Mach port, and the kernel wakes up the blocked thread and delivers the message. The programs sees this as the mach_msg function returning.

    There are all kinds of under-the-hood ways to get a Mach message sent to you. For example, if you set up an NSTimer, that will probably work its way down to the mk_timer_arm system call, which simply causes a Mach message to get sent somewhere after a certain amount of time. The run loop, then, is not a fancy infinite loop so much as it is a dispatcher and a mapping between the kernel and the Cocoa (or Core Foundation) frameworks.

    It should go without saying that if the kernel has suspended your thread because you’re waiting on a message, that you aren’t actually using any CPU time, which is why your application appears to be idle. Why bother with an infinite loop when you have a modern kernel?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.