BUG: glfwPostEmptyEvent() does not unblock glfwWaitEvents();

A few days ago I thought I experienced a bug but then it disappeared and I was unable to recreate it.

However, I have now isolated the bug.

The bug is experienced when calling glfwPostEmptyEvent() where it should cause the blocked thread that called glfwWaitEvents() to return but in this instance it does not.

To recreate the bug…
create a thread that initialises glfw then calls glfwWaitEvents()
create another thread that calls glfwPostEmptyEvent() and the previous thread does not return.

This is a bit of a pain because many of the functions dictate that only the thread that called the initialisation of glfw can be used and if the thread does not return I am unable to use those functions including opening a window.

Opening a window seems to provide the intended behaviour but I have many initialisation routines that require glfw to be initialised before any windows are opened and if any of these fail at that moment the application simply hangs because the thread does not return.

Please look into this.

Many Thanks.

You should only init and call glfwWaitEvents() from the main thread - so this could be the source of your problems. The main thread here being the thread which your program starts with. See the section on thread safety.

It’s also possible that you are calling glfwPostEmptyEvent before you init GLFW so you may need to add some form of synchronization.

Comment removed by mod

You should post exact code that does not work for you.
I’ve tried following code and it worked just fine on Windows:

#include <thread>
#include <chrono>
#include <GLFW/glfw3.h>
#include <stdio.h>

int main()
{
	glfwInit();
	glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
	GLFWwindow* w = glfwCreateWindow(100, 100, "W", NULL, NULL);
	glfwPollEvents();

	fprintf(stderr, "Starting thread\n");

	std::thread t([]()
	{
		using namespace std::chrono_literals;
		fprintf(stderr, "Waiting in thread for 3 seconds\n");
		std::this_thread::sleep_for(3s);
		fprintf(stderr, "Posting empty event\n");
		glfwPostEmptyEvent();
		fprintf(stderr, "Thread finished\n");
	});

	fprintf(stderr, "Waiting for event\n");
	glfwWaitEvents();
	fprintf(stderr, "OK, continuing in main\n");

	t.join();
	return 0;
}

It prints out:

Starting thread
Waiting for event
Waiting in thread for 3 seconds
Posting empty event
Thread finished
OK, continuing in main

As you can see - main thread returns from glfwWaitEvents function, exactly how it is written in documentation.

Thanks @mmozeiko - I’ve also tried this without creating a window (removed 3 lines after glfwInit()) since I think that might be what @pennyzenny is trying to do, on Windows 11 and Ubuntu 20.04.4, and this also works.