来自非主线程的 glfwInit()

glfwInit() from non-main thread

LWJGL 3 的 glfwInit() JavaDoc 指出:

This function must only be called from the main thread.

不过,我在宏机 运行 windows 10 上测试过,我发现调用哪个线程 glfwInit() 并不重要,因为只要它是与任何其他 glfw 调用相同的线程。

文档有误,还是只适用于某些机器或操作系统?

I've found that it does not matter which thread glfwInit() is called on

首先。仅仅因为某些事情似乎有效,并不意味着它会继续有效。换句话说,某些特定事件可能最终导致您尚未测试的应用程序崩溃。

This function must only be called from the main thread.

这里重要的是 GLFW documentation 从未指定 "main thread" 的含义。

引用 GLFW 的开发者和维护者 Camilla Löwy / elmindreda

Cocoa has a single event queue that may only be accessed from the main thread, and the same goes for most window operations. Win32 has one event queue per thread and windows are tied to the queue of the thread that created them. X11 has a single queue and event processing and window operations may be done from any thread. The limitation imposed by GLFW is to ensure that programs are portable. This limitation is not enforced, i.e. the library does not try to prevent you from shooting yourself in the foot. If loading or rendering blocks your program, put it in another thread. Everything you need to render and swap buffers is thread-safe on all platforms for this reason.

The limitation imposed by GLFW is to ensure that programs are portable. This limitation is not enforced, i.e. the library does not try to prevent you from shooting yourself in the foot.

简而言之。 Cocoa 不会开心。但是在 Windows 上,假设 window 和 OpenGL 上下文是在同一个线程上创建的,那么就不会有任何问题。

但最终还是遵守规则 确保程序可移植.