C++/SDL 线程 - 事件崩溃
我正在开发一个贪吃蛇游戏(位于 https://github.com/RobotGymnast/Gingerbread/树/事件线程)。最初,所有内容(图形、事件、游戏逻辑更新、物理)都是从“主”线程调用的。然后我开始多线程(使用boost线程)。这非常简单,但我最近将图形显示逻辑拆分到一个新线程中,该线程在其本地堆栈空间中分配屏幕对象。然后我将事件检测和事件处理逻辑拆分到一个新线程中。然后我的屏幕就不再出现了。从我的命令行输出来看,一切仍然正常,只是屏幕停止出现。事实证明它挂在我的 SDL_SetVideoMode() 调用上。
我通过在“主”线程中分配屏幕对象并传入对图形线程的引用来修复此问题。由于某种原因,从事件逻辑在新线程中分配屏幕对象会产生问题。
自此修复后,事件检测和事件处理不再起作用。事件检查仍在进行,例如 SDL_PollEvent(),但它们根本没有拾取任何事件(键盘、鼠标等)。
我怀疑 SDL 可能会进行一些幕后线程同步,但我一直在使用 boost 线程。这会是一个问题吗? SDL 线程的限制相当大,我不想切换。
以前有人遇到过这个问题吗?有什么建议吗?
I have a Snake game in-development (up at https://github.com/RobotGymnast/Gingerbread/tree/eventThreaded). Initially, everything (graphics, events, game logic update, physics) were called from a "main" thread. Then I started multithreading (using boost threads). It's been pretty straightforward, but I recently split the graphics display logic into a new thread, which allocated the screen object in its local stack space. Then I split my event-detection and event-handling logic into a new thread. Then my screen stopped appearing. Judging by my command-line output, everything still worked fine, just the screen stopped appearing. It turned out it was hanging on my SDL_SetVideoMode() call.
I fixed this by allocating my screen object in the "main" thread, and passing in a reference to the graphics thread. For some reason, allocating the screen object in a new thread from the event logic was creating problems.
Since this fix, the event-detection and event-handling no longer works. The event checks are still being made, e.g. SDL_PollEvent(), but they're not picking up any events at all (keyboard, mouse, etc.).
My suspicion is that SDL might do some behind-the-scenes thread syncing, but I've been using boost threads. Could this be a problem? SDL threads are rather restrictive, and I'd rather not switch.
Anybody had this issue before? Any recommendations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 SDL,但在几个窗口子系统上(我相信在 X 和 Win32 上),您无法修改与图形对象或小部件相关的任何内容,除了最初创建该图形对象/小部件的线程之外。
它看起来不像(根据我有限的 10 秒谷歌搜索)SDL 从你那里抽象了这一点——你只需要从创建它们的线程中修改与图形相关的对象。否则就会引发奇怪的行为。
I'm not sure about SDL, but on several windowing subsystems (I believe on both X and Win32), you cannot modify ANYTHING related to a graphics object or widget, except from the thread which initially created that graphics object/widget.
It doesn't look like (to my limited 10 second google search) SDL abstracts that bit from you -- you'd need to only modify graphics related objects from the thread that created them. To do otherwise is to invite strange behavior.
由于各个平台上的一些技术考虑,图形显示逻辑几乎应该始终位于主线程中。
同样,事件处理(至少在低级别)应该在主线程中,因为事件可以发布到特定线程而不是进程。
在大多数情况下,我建议不要从主线程以外的任何地方调用任何 SDL 函数,除了不在共享状态上运行的函数之外。
Graphics display logic should almost always be in the main thread, due to some technical considerations on various platforms.
Similarly, the event handling (at least at the low level) should be in the main thread, as events can be posted to specific threads rather than processes.
For the most part I would recommend not calling any SDL functions from anything other than the main thread apart from ones that don't operate on shared state.