The glfw documentation on Thread safety says the following:
Most GLFW functions must only be called from the main thread (the thread that calls main), but some may be called from any thread once the library has been initialized. Before initialization the whole library is thread-unsafe.
Is there any specific reason that thread has to be the main thread and not a secondary one ? If so,what would that be ? As far as I know, windows lets you have the window on a non-main thread. I’m curious about X11 and wayland…
The reason I’m asking is because I want to wrap glfw and some other platforms such as android into a higher level api and it’s easier to just fire off glfw on a secondary thread when creating the window, but if it has to be the main thread, that really adds a lot of constraints.
Hi @constantitus,
This response Multithreading GLFW? - #5 by elmindreda covers the reasons behind why some GLFW functions need to be called from the main thread.
I see, so the gist of it is that macos is the only platform that imposes this limitation and I’m fine with putting glfw on a non-main thread since I don’t care about macos. Am I right ?
This would likely work well on other platforms than Mac OS X, but since it’s part of the GLFW spec there’s always a possibility something else gets introduced in future which might require the main thread, though I think that’s unlikely.
Working around this limitation usually isn’t too hard since you can still move rendering etc. to another thread.
Yes, I do rendering on the main thread and have glfw on a second thread. The main reason I wanted to isolate the window on another thread is because Windows blocks the window thread on resize. Otherwise I wouldn’t have bothered with polling events on another thread.
The approach with GLFW is to do the reverse - put your rendering on a second thread and then GLFW functions which require the main thread can be used on the main thread.