Thursday, June 28, 2018

Run, RunLoop, Run

Nicolas Bouilleaud (tweet):

Put simply, a run loop is a messaging mechanism, used for asynchronous or interthread communication. It can be seen as a post box that waits for messages and delivers them to recipients.

A run loop does two things:

  • wait until something happens (e.g., a message arrives),
  • dispatch that message to its receiver.


The most important feature of CFRunLoop is the CFRunLoopModes. CFRunLoop works with a system of “Run Loop Sources”. Sources are registered on a run loop for one or several modes, and the run loop itself is made to run in a given mode. When an event arrives on a source, it is only handled by the run loop if the source mode matches the run loop current mode.

Greg Parker:

1. Write an event-handling loop: it waits for events—user input, timers, etc—and calls the handlers registered for those events.

2. Put the loop in an opaque box. That’s a run loop object.

I imagine it as a jack-in-the-box. You turn the crank and calls to event handlers pop out.

Comments RSS · Twitter

Leave a Comment