Dmytro
Dmytro

Reputation: 5213

Does wglMakeCurrent, drawing, and multithreading cause a race?

If I have two opengl rendering threads, each of which has a pattern of setting its context, doing drawing at a fixed framerate. Will there be a race?

I am worried that the following will happen:

  1. Thread 1 calls wglMakeCurrent
  2. Thread 2 calls wglMakeCurrent
  3. Thread 2 draws
  4. Thread 1 draws

In turn both threads draw on the context of the second thread.

Should I be using locks whenever I set a rendering context, and draw to ensure the operation doesn't interfere with other rendering threads?

Upvotes: 2

Views: 675

Answers (1)

sterin
sterin

Reputation: 1958

As long as each thread has its own context, you can draw in both threads safely.

Only if you want to share a single context between the threads you have to ensure that the context is current only in a single thread at a time.

The documentation of wglMakeCurrent() sets up the rules:

A thread can have one current rendering context. A process can have multiple rendering contexts by means of multithreading.

A thread must set a current rendering context before calling any OpenGL functions. Otherwise, all OpenGL calls are ignored.

A rendering context can be current to only one thread at a time. You cannot make a rendering context current to multiple threads.

An application can perform multithread drawing by making different rendering contexts current to different threads, supplying each thread with its own rendering context and device context.

Upvotes: 1

Related Questions