Upon further examination and debugging, I have found the following issues:
When adding mutexes to stop race condition, the condition improves. In my previous attempt I had forgotten to unlock the mutex after exiting loops. Although this lead to the program working as expected, it still crashed after a couple of seconds running. The error suggested the use of XInitThreads.
The program is now running as expected after the following steps:
I installed the libX11 package (Fedora):
sudo dnf install libX11-devel.x86_64 -y
Added the library to the code
#include <X11/Xlib.h>
Added the XInitThreads() function call before creating a new thread
int main() {
// ...
XInitThreads();
std::thread eventThread(/*..*/);
// rest of code...
}
Compiled with the additional flag
-lX11
This seems to have worked, even after I removed all the mutexes, although I am not entirely sure why.
I will do some further research on the what XInitThreads does and will report back with further details.