kqueues and Runloops

You can use kqueues in your Cocoa and Carbon applications to monitor OS events of interest by making a thread that sits on kevent(), or you can use the runloop.

You can create a thread that contains the kevent() call. If given no timeout, kevent() will block the thread until something interesting happens. That thread, after it wakes up, can notify the application’s main thread about what happened or do whatever work is appropriate.

It is safe, though, to manipulate a file descriptor in one thread while another thread is monitoring it with a kqueue. If the file is closed, the kernel atomically removes any pending events associated with this file descriptor in the process. However, there is one gotcha. If a file descriptor ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.