我可以从第二个线程添加 pygame 事件吗

编程入门 行业动态 更新时间:2024-10-27 10:21:42
本文介绍了我可以从第二个线程添加 pygame 事件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,我在很多地方都读到 pygame 事件处理必须在主线程中完成.我想这样做,但我的问题是,我可以从不同的线程向事件队列添加事件吗?

Well I have read at various places that the pygame event handling must be done in the main thread. I want to do that but my question is, can I add events to the event queue from a different thread?

我想从不同的线程调用 pygame.event.post(myEvent) 而不是在主循环中处理事件.这可能吗?

I want to call pygame.event.post(myEvent) from a different thread and than handle the event in the main loop. Is this possible?

澄清一下,我想为异步网络 i/o 运行一个单独的线程.当新消息到达时,线程会在事件队列中放置一个事件,以表示有事情要做.

edit: To clarify, I want to run a separate thread for asynchronous network i/o. When a new message arrived the thread would then put an event in the event queue to signal there is something to do.

推荐答案

是时候进行一些侦探工作了!

Time for some detective work!

在 event_post 的源代码"noreferrer">event.c 表示 C 函数使用 SDL 调用 SDL_PushEvent,而不自行检查线程安全.但是,SDL_PushEvent 的文档说:

Looking at the source for event_post in event.c indicates that the C function uses the SDL call SDL_PushEvent, without checking for thread-safety on its own. However, the documentation for SDL_PushEvent says:

该函数是线程安全的,可以安全地从其他线程调用.

This function is thread safe, and can be called from other threads safely.

所以看起来确实是线程安全的.

So it seems that it is indeed thread-safe.

更多推荐

我可以从第二个线程添加 pygame 事件吗

本文发布于:2023-07-29 08:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二个   线程   事件   pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!