I am creating an opengl application and it works well when I didn't use multithreading. The original code is shown below:
void Controller::BeginLoop()
{
while (!glfwWindowShouldClose(_windowHandle)) {
/* Render here */
Render();
/* Swap front and back buffers */
glfwSwapBuffers(_windowHandle);
/* Poll for and process events */
glfwPollEvents();
}
}
int main()
{
//do some initialization
g_controller->BeginLoop();
}
The above code works well, however, when I tried to put the eventpolling and rendering into two different threads, the OpenGL won't draw anything in the window. Below is the multithread code I used:
void Controller::BeginLoop()
{
while (!glfwWindowShouldClose(_windowHandle)) {
glfwMakeContextCurrent(_windowHandle);
/* Render here */
Render();
/* Swap front and back buffers */
glfwSwapBuffers(_windowHandle);
}
}
void Render(int argc, char **argv)
{
::g_controller->BeginLoop();
}
int main()
{
std::thread renderThread(Render, argc, argv);
while (true) {
glfwPollEvents();
}
renderThread.join();
return 0;
}
In the Render function, I do some physics and draw the result points onto the window. I totally have no idea what is going wrong.