user1920731
user1920731

Reputation: 3

Win32 Message Loop, OpenGL Context in different threads

Is there anything wrong with creating a window in a separate thread, which will also contain the message loop, then creating an OpenGL Context in another thread?

Upvotes: 0

Views: 1037

Answers (2)

datenwolf
datenwolf

Reputation: 162164

What you want to do is perfectly possible. Even better, OpenGL contexts can migrate between threads and even be used with multiple windows as long as their pixel format is compatible. The one constraint is, that a OpenGL context can be bound in only one thread at a time and that only a unbound context can be bound.

So you could even create the window and the context in one thread, then unbind the context, create another thread and re-bind the context to the window in the secondary thread. No problem there.

The only thing you must be aware of is, that OpenGL itself doesn't like to be multithreaded. The API itself is more or less thread safe, as only one context can be bound to a thread at a time. But all the bookkeeping required if OpenGL operations spawn over several threads may trigger nasty driver bugs and also has a certain performance hit.

Upvotes: 0

Jari Komppa
Jari Komppa

Reputation: 604

You should be able to get it to work, if you're careful. See the parallel opengl faq.

Q: Why does my OpenGL application crash/not work when 
   I am rendering from another thread?
A: The OpenGL context is thread-specific. You have to 
   make it current in the thread using glXMakeCurrent, 
   wglMakeCurrent or aglSetCurrentContext, depending on 
   your operating system.

Upvotes: 4

Related Questions