tags:

views:

404

answers:

4

I once tried to open a GLUT window from a sub-thread and got lots of nasty problems. I remember this post on lists.apple.com:

GLUT functions may only be called from the application's main thread

Has anything changed in this regard with GLUT on Mac OS X ? Is there a thread-safe GLUT that let's you open windows from any thread ?

If GLUT is not an option, is there a tiny library that replaces GLUT and would work from any thread ?

[edit]

Here is the result of my tests triggered by the various solutions proposed as answers:

  • GLFW looked nice but did not compile (current branch is 3 years old)
  • Agar was another pretender but it's too big for the tiny need I had
  • SDL is not BSD-license compatible and it's a huge library for code that should fit on a single file
  • GLUT cannot run in any thread.

I decided to reinvent the wheel (yes, that's good sometimes) and the final class is just 200 lines of code. It let's me open and close a window from any thread (openGL draw in new thread) and I have full control on vertical sync and such (SDL uses double buffering = slow for openGL). I had to trick around the NSApp to properly start and stop the application (which does not use an event loop otherwise).

To those telling me that OpenGL is not thread-safe, that's not exactly true: you can run multiple OpenGL threads and the draw commands will be executed in the OpenGL state assigned to that thread. OpenGL is thread-specific.

If anyone needs some bare-bones code to create OpenGL windows using Cocoa: gl_window.mm

+2  A: 

GLUT is not thread safe. You'll need locking primitives with whatever solution you choose to implement. I'd recommend setting up your own GL view in Cocoa and rewriting the plumbing that GLUT provides.

Take a look at SDL as a modern GLUT replacement. It should give you all the cross-platform you want. As far a cross-platform threading, Boost provides a portable library.

Pestilence
All the code has to run on Mac, Windows and Linux. If there is a plumber out there that avoids me the pain to rewrite a glut equivalent, that would be great.
Gaspard Bucher
http://www.opengl3.org/resources/libraries/windowtoolkits/
Pestilence
A: 

You are confused. On the Mac, while it is possible to create windows on any thread, all windows share a single application wide event queue - which is managed by the main thread.

glut can, as a result, only do its modal loop on the MacOS X main thread and as glut is not thread safe, all subsequent access must happen in the same thread.


For the hard of comprehension: Mac Dev Center: Thread Programming Guide: Thread Safety Summary

Yes, you can create NSRunLoops on every thread. That is because every thread does have a run loop. That does not change however the fact that NSApplication run is responsible for dispatching events to NSViews and NSWindows and NSViews can ONLY be used from the main thread.

So, I repeat my original point. NSView can only be called from the main thread. Hence its runLoop must be on the main thread.

Chris Becke
That's not true. You can start an `NSRunLoop` on any thread.
Gaspard Bucher
A: 

As a replacement for GLUT, have a look at GLFW. It's similar in purpose and workings, but better. And it does not have a glfwMainLoop that your program is stuck with; it allows you full control. Never since I discovered GLFW have I had a need to switch back to GLUT.

Note that GLFW is not thread-safe, in the sense that it is unsafe to call GLFW functions from different threads (FAQ entry). However, as long as you call all GLFW functions from the same thread, it's your choice which thread that will be.

Thomas
GLFW looked nice but the stable branch is 3 years old and it does not compile (lots of deprecation warnings and errors).
Gaspard Bucher
A: 

Not only is GLUT is not thread safe, but OpenGL is a state machine, and therefore isn't thread safe. Having said that, you can have multithreaded applications that use OpenGL. Just make sure all your OpenGL calls are made from the same thread.

The next step up from GLUT on Mac OS X is the Cocoa OpenGL Sample Code. This is a true Cocoa application that demonstrates the Cocoa way of setting up an OpenGL window, with interactivity using the Cocoa event model. From this starting point, it's fairly easy to add code to handle your program logic in a separate thread (or threads) from your OpenGL drawing code.

Mr. Berna