< Previous PageNext Page > Hide TOC

Multithreading and OpenGL

Each process in Mac OS X is made up of one or more threads. A thread is a stream of execution that runs code for the process. You can improve application performance and enhance the perceived responsiveness of the user interface when you set up your application to use multiple threads. On computers with one processor, multithreading can allow a program to execute multiple pieces of code independently. On computers with more than one processor, multithreading can allow a program to execute multiple pieces of code simultaneously.

Multithreading, however, is not the solution for all performance issues. When it is a possible solution, it enhances performance only when it's set up correctly. Getting multithreading to work properly in an OpenGL application requires advanced programming techniques—the OpenGL API is not inherently thread-safe. If you want to make your OpenGL program multithreaded, read this chapter to get started, then roll up your sleeves. Be prepared to undertake a lot of detective work if things go wrong. In threaded applications, the cause of the problem is often difficult to isolate.

In this section:

Program Design
Guidelines for Threading OpenGL Applications
When Things Go Wrong
Threading APIs
See Also


Program Design

You'll have the best chance of success with multithreading if you design your program with threading in mind. It's difficult, and often risky, to retrofit an existing OpenGL application to use multiple threads. Before you write any threading code, choose a strategy for dividing work among threads.

Consider using one of the following strategies for your OpenGL application:

Applications that move OpenGL onto a separate thread are designed as shown in Figure 11-1. The CPU writes its data to a shared space, accessible to OpenGL. This design provides a clear division of labor and is fairly straightforward to implement. You can use this design to load data into your application on one thread, and then draw with the data on the other thread.


Figure 11-1  CPU processing and OpenGL on separate threads

CPU processing and OpenGL on separate threads

The Apple-specific OpenGL APIs provide the option for sharing data between contexts. You can leverage this feature in a threaded application by creating a separate thread for each of the contexts that share data, as shown in Figure 11-2. Shared resources are automatically set up as mutual exclusion (mutex) objects. Notice that Thread 2 draws to a pixel buffer that is linked to the shared state as a texture. Thread 1 can then draw using that texture.


Figure 11-2  Two contexts on separate threads

Vertex and texture processing on separate threads

Guidelines for Threading OpenGL Applications

Follow these guidelines to ensure successful threading in an application that uses OpenGL:

Note: The guidelines in this section are specific to OpenGL applications. Any threading code that you write also needs to comply with general threading practices. You can find general resources for thread programming in the “See Also” section.

When Things Go Wrong

If you don't set up threading correctly, you'll most likely see your application freeze or crash. Things typically go wrong when your application introduces a command to the graphics processor that violates threading practices. The bad command will cause the processor to hang. The CPU blocks against that, causing any drawing onscreen to stop and the spinning wait cursor to appear.

You can use OpenGL Profiler to check thread safety in OpenGL. In the breakpoints window, set the "Break on thread error" option to check whether a problem is due to a thread error.

Threading APIs

The following APIs are available for creating threaded applications in Mac OS X:

See Also

The OpenGL sample code project Vertex Optimization (available from Sample Code > Graphics & Imaging > OpenGL) has an option to run as a multithreaded application.

Multithreading programming guides and reference documentation:



< Previous PageNext Page > Hide TOC


© 2004, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-06-09)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.