gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:12 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com