c++ - 在类中启动的第二个线程中的 OpenGL

标签 c++ multithreading opengl

我有一个 Window 类,它创建一个 OpenGL 窗口并启动渲染循环。如果我在主线程中创建一个窗口,它就可以工作。

void threadTest() {  
    Window w(800, 600, "Hallo Welt");  
}

int main() {
    std::thread t(threadTest);
    sleep(5);
    t.join();
    return 0;
}  

但是,如果我在我的 Engine 类中创建线程,它会出现段错误。

void Engine::createWindow(unsigned int width, unsigned int height, const std::string & title) {
    m_rendering = new std::thread(&Engine::windowThread, * this, width, height, title);
}

void Engine::windowThread(unsigned int width, unsigned int height, const std::string & title) {
    Window w(width, height, title);
}

int main() {
    Engine e;
    e.createWindow(800, 600, "Hallo Welt");
    sleep(5);
    return 0;
}  

我做错了什么?

最佳答案

尝试使您的线程函数静态化。

即:

 static void Engine::windowThread(
     unsigned int width, 
     unsigned int height, 
     const std::string & title) 
 {

     Window w(width, height, title);
 }

如果您需要传递类实例,则为 this 指针使用一个额外的参数。

关于c++ - 在类中启动的第二个线程中的 OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21685932/

相关文章:

c++ - 在 Linux 上为 OpenGL 4.2 设置开发环境(无法获取 gl.h)

c++ - 自动向下转换指向派生对象的指针

c++ - 从 Huffman Tree Minheap 弹出/删除节点

非多线程程序中的java.util.ConcurrentModificationException

android - 每个Android进程都有自己的线程吗?

opengl - CUDA 立方体贴图纹理

c++ - 在 C++ 标准中,哪里声明了 const 右值引用不绑定(bind)到左值?

c++ - 你能取消引用整数文字吗?

java - 简单多线程程序中的对象共享

c++ - 在 OpenGL 中存储模型矩阵的最佳位置?