0

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?

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

2 Comments

The window is created in a separate thread, whereas the context is created in the main thread, where drawing will be done. Is it necessary to make the context current?
My guess would be no, as long as you're doing all of your opengl stuff in one thread.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.