过剩,不同线程 C++ 中的不同上下文

Glut, different context in different thread C++

我试图在不同的上下文中使用不同的过剩 window,我的意思是,每个 window 都有自己的显示回调,空闲...

所以我有一些关于过剩的问题:

第一次尝试:

glutInit在程序开始,线程在->之后 当我关闭 window 时,发生错误:

freeglut  ERROR:  Internal <Event Handler> function called without first calling 'glutInit'.

第二次尝试:

每个线程中的glutInit: 当第二个线程启动时:

freeglut illegal glutInit() reinitialization attempt

GLUT 在设计时并未考虑线程安全(甚至根本不支持多线程)。所以我担心,使用 GLUT 你将无法实现你想要的。不过,GLFW在这件事上似乎要好很多:

Thread safety

Most GLFW functions may only be called from the main thread, but some may be called from any thread. However, no GLFW function may be called from any other thread until GLFW has been successfully initialized on the main thread, including functions that may called before initialization.

The reference documentation for every GLFW function states whether it is limited to the main thread.

The following categories of functions are and will remain limited to the main thread due to the limitations of one or several platforms:

  • Initialization and termination
  • Event processing
  • Creation and destruction of window, context and cursor objects

这部分对你来说似乎很重要:

Rendering may be done on any thread. The following context related functions may be called from any thread:

  • glfwMakeContextCurrent
  • glfwGetCurrentContext
  • glfwSwapBuffers
  • glfwSwapInterval
  • glfwExtensionSupported
  • glfwGetProcAddress

[...]

GLFW uses no synchronization objects internally except for thread-local storage to keep track of the current context for each thread. Synchronization is left to the application.

来源:GLFW Doc - Thread Safety