c - glDisplayFunc(RenderScene) 回调是否会在以下代码中导致竞争条件?

标签 c opengl race-condition

我正在阅读 OpenGL Superbible第 4 版。在第 2 章中,示例代码设置回调后跟清除颜色如下:

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);
}

这里是否存在竞争条件,glClear 可能在 glClearColor 之前执行?

最佳答案

这不是竞争条件,因为 glutMainLoop() 在同一个线程中运行并且调用 glDisplayFunc() 不会调用任何 GL 函数(它只保存指向回调的指针)。

来自文档:

glutMainLoop enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered

OpenGL 只能渲染到在同一线程中创建的 GL 上下文。因此,对 glClearColor()RenderScene() 的调用将在同一个线程中调用。由于对 glutMainLoop() 的调用稍后会在您的 main() 中调用,因此 glClearColor() 将严格在 glClear 之前调用()RenderScene() 中。

关于c - glDisplayFunc(RenderScene) 回调是否会在以下代码中导致竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293959/

相关文章:

c - 如何检查数组的元素是否被占用? +1 更多

c - 寻找素数。 6k+1 6k-1 以外的质数形式

c++ - 如何在 VS2010 Express 中独立发布/调试静态库?

windows - 从 GPU 获取完整的桌面截图

Java字节数组多线程

c++ - 示例 2.1,K&R : Wrong symbolic constants value for LONG_MIN and LONG_MAX?

linux - ld 不链接 Linux 上的 OpenGL

rabbitmq - AMQP/RabbitMQ - 如何避免竞争条件

Java 并发实践 : race condition in BoundedExecutor?

c 字符指针问题