Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreaded X11 application and OpenGL

I'm trying to create a multithreaded opengl application with libx11 - with one separate thread per window, and one manager thread.

I have an event loop in the manager thread:

while(true)
  while(XQLength(mPlatformData->display)){
    XNextEvent(mPlatformData->display, &event);
    std::cout << "event" << std::endl;
  }
}

This is a great event loop for single threaded applications, but with this multithreaded setup strange things happen.

When I'm creating a window, I need to disable the event queue, or GLXMakeCurrent will just hang - my entire thread stops, and does nothing.

I can't find much information about multithreaded X11 applications on the net, should I handle my events differently?

like image 843
Dutow Avatar asked Jun 19 '11 12:06

Dutow


1 Answers

It is known that Xlib has several unfixable runtime issues that manifest in concurent access situations. I'm guessing you're running into exactly one of those.

This is one among the reasons why Xcb was created in the first place: Fix the problems of Xlib. GLX is specified against Xlib so this might seem like a show stopper when it comes to OpenGL. However there is a Xlib wrapping around Xcb and one can safely use that to interface with GLX and still use Xcb for the rest of the program: http://xcb.freedesktop.org/opengl/

I see two possible solutions:

  1. Put a XLockDisplay/Mutex around XNextEvent and the GLX calls each; you don't have to lock for ordinary OpenGL, just the functions prefixed glX....

  2. Use Xcb to get runtime correct behaviour and follow the guide I linked above to make it work with OpenGL/GLX.

like image 158
datenwolf Avatar answered Oct 17 '22 23:10

datenwolf