多线程 X11 应用程序和 OpenGL [英] Multithreaded X11 application and OpenGL

查看:44
本文介绍了多线程 X11 应用程序和 OpenGL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 libx11 创建一个多线程 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.

当我创建一个窗口时,我需要禁用事件队列,否则 GLXMakeCurrent 将挂起 - 我的整个线程停止,什么也不做.

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.

我在网上找不到关于多线程 X11 应用程序的很多信息,我应该以不同的方式处理我的事件吗?

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

推荐答案

众所周知,Xlib 有几个无法修复的运行时问题,这些问题表现在并发访问情况下.我猜你正好遇到了其中之一.

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.

这就是最初创建 Xcb 的原因之一:修复 Xlib 的问题.GLX 是针对 Xlib 指定的,因此当涉及到 OpenGL 时,这似乎是个障碍.然而,有一个 Xlib 环绕 Xcb,可以安全地使用它来与 GLX 接口,并且仍然使用 Xcb 来处理程序的其余部分:http://xcb.freedesktop.org/opengl/

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. XNextEvent 周围放置一个 XLockDisplay/Mutex,GLX 会调用每个;您不必为普通的 OpenGL 锁定,只需使用前缀 glX... 的函数即可.

  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....

使用 Xcb 获得运行时正确的行为,并按照我上面链接的指南使其与 OpenGL/GLX 配合使用.

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

这篇关于多线程 X11 应用程序和 OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆