I’m reading OpenGL Superbible 4th ed. In Chapter 2, the example code sets up the callback followed by the clear color as follows:
main()
{
//...
glDisplayFunc(RenderScene);
SetupRC();
//..
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
Is it posisble that we have a race condition here, where glClear might be executed before glClearColor?
It is not a race condition, because
glutMainLoop()runs in the same thread and calling theglDisplayFunc()does not invoke any GL functions (it only saves the pointer to your callback).From the docs:
OpenGL can only render to the GL contexts created in the same thread. Thus the calls to
glClearColor()andRenderScene()will be called in the same thread. Since the call toglutMainLoop()is called later in yourmain(), theglClearColor()will be called strictly beforeglClear()inRenderScene().