【发布时间】:2012-10-10 23:02:47
【问题描述】:
我编写了一个 OpenGL 程序,它以以下方式运行:
Main:
- Initialize SDL
- Create thread which has the OpenGL context:
- Renderloop
- Set camera (view) matrix with glUniform.
- glDrawElements() .... etc.
- Swapbuffers();
- Main SDL loop handling input events and such.
- Update camera matrix of type glm::mat4.
这就是我将相机对象传递给处理 opengl 的类的方式。
Camera *cam = new Camera();
gl.setCam(cam);
在哪里
void setCam(Camera *camera) {
this->camera = camera;
}
对于在 opengl 上下文线程中进行渲染,会发生这种情况:
glm::mat4 modelView = camera->view * model;
glUniformMatrix4fv(shader->bindUniform("modelView"), 1, GL_FALSE, glm::value_ptr(modelView));
在我的 SDL 和其他东西是句柄的主程序中,然后我重新计算视图矩阵。在我没有使用任何互斥锁的情况下,他的工作正常。这是正确的吗?
另一方面,我通过“上传队列”将对象添加到我的场景中,在这种情况下,我必须在向其添加项目时互斥锁我的上传队列向量(向量类类型),否则程序会崩溃。
总结:我在另一个线程中重新计算我的矩阵,然后在没有任何互斥锁的 opengl 线程中使用它。为什么会这样?
编辑: 我认为我的问题类似于这里提出的问题:
1) Should I lock a variable in one thread if I only need it's value in other threads, and why does it work if I don't?,仅在我的情况下,它更简单,只需更改一个矩阵。
2) Do I need a lock when only a single thread writes to a shared variable?
【问题讨论】:
-
OpenGL 中没有
gl.setCam函数。或者 SDL。在不知道gl.setCam做什么或它如何处理其指针参数的情况下,无法回答您的问题。 -
对不起。用更多细节更新了代码。
标签: c++ multithreading thread-safety