gartenriese
gartenriese

Reputation: 4356

OpenGL in a second thread, which is started in a class

I have a Window class which creates a OpenGL Window and starts the rendering loop. If I create a Window in a thread in main, it works.

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

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

However, if I create the thread in my Engine class, it segfaults.

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

What am I doing wrong?

Upvotes: 1

Views: 182

Answers (1)

Lother
Lother

Reputation: 1797

Try making your thread function static.

ie:

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

     Window w(width, height, title);
 }

If you need to pass the class instance, then use an extra param for the this pointer.

Upvotes: 1

Related Questions