我可以从第二个线程添加 pygame 事件吗 [英] Can I add pygame events from a second thread

查看:65
本文介绍了我可以从第二个线程添加 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 事件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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