简体   繁体   English

我的游戏需要多线程吗?

[英]Do I need multithreading for my game?

I'm trying to make a simple breakout game with opengl es, on Android, and I can't decide wether I have to use a seperate thread for certain game logic stuff, like updating the game and collission detection. 我试图用Android上的opengl进行一个简单的突破游戏,我无法决定是否必须使用单独的线程来处理某些游戏逻辑,比如更新游戏和碰撞检测。 I've prototyped a simple one: Drawing and Updating parallel of eachother in their own thread. 我做了一个简单的原型:在自己的线程中绘制和更新彼此的并行。 It brought more problems than I thought, and so I thought: 它带来的问题比我想象的要多,所以我想:

Do I need more than one thread? 我需要多个线程吗?

Since a breakout game for example, has like 6X5 grid of blocks, and for each block it needs to test 例如,由于突破游戏,有6X5网格块,并且每个块需要测试
Does the ball touch this brick? 球会碰到这块砖吗? And about 30 times a second. 而且每秒约30次。 If I put this code in my main Drawing loop: OnDrawFrame(GL10) I'm afraid it will cost alot of rendering time. 如果我把这段代码放在我的主要绘图循环中:OnDrawFrame(GL10)我恐怕会花费很多渲染时间。
So can I just return to single threaded game, or not? 那么我可以回到单线程游戏吗? (since Android devices are not that powerful compared to ..pc's. (因为Android设备与..pc相比并不那么强大。

I'm trying to make a simple breakout game 我正试图制作一个简单的突破游戏

No, you do not need to thread anything. 不,你不需要任何东西。

Threading is not for "simple" tasks. 线程不是为了“简单”的任务。 Threading is for tasks where you can reasonably expect significant problems with performance, such that you would actually gain something by going multithreaded. 线程是针对您可以合理地预期性能存在重大问题的任务,这样您实际上可以通过多线程获得某些东西。

What you will gain by threading a Breakout clone is a coding headache. 通过线程化Breakout克隆你将获得的是一个编码问题。 Nothing more. 而已。 It simply isn't significant enough to require it, and if you don't need threading, you should almost never use it. 它根本不足以要求它,如果你不需要线程,你几乎不应该使用它。

Yes, you should definitely not be using a single thread for your rendering and calculations. 是的,你绝对不应该使用单个线程进行渲染和计算。 As an aside you might find the IO talk on realtime android games here useful. 顺便说一下,你可能会发现在这里实时安卓游戏的IO谈话很有用。

Since a breakout game for example, has like 6X5 grid of blocks, and for each block it needs to test 例如,由于突破游戏,有6X5网格块,并且每个块需要测试

So you have to perform 30 circle-recangle tests per update. 因此,每次更新必须执行30次循环重新测试。 I think current computers are fast enough to do this a few hundred of thousand times a second. 我认为目前的计算机速度足够快,每秒可以达到几十万次。

Multithreading makes some things easier, and keeping rendering and game logic in separate threads is a common practice. 多线程使一些事情变得更容易,并且将渲染和游戏逻辑保持在单独的线程中是一种常见的做法。 But in your case the overhead that comes with it is simply not worth the effort. 但在你的情况下,它带来的开销根本不值得努力。 Also a gain in performance is only achieveable on multicore CPUs. 此外,性能的提升只能在多核CPU上实现。

Multithreading make sense if your application is supposed to run on multicore systems. 如果您的应用程序应该在多核系统上运行,那么多线程是有意义的。

When # of threads exceeds number of cores it is called Oversubscription, which is usually bad effect in threaded applications because it gives additional overhead. 当线程数超过核心数时,它被称为Oversubscription,这在线程应用程序中通常会产生不良影响,因为它会带来额外的开销。

Sometimes oversubscription is intended when you expect some threads to be waiting for considerable amount of time (eg read/write operation on filesystem). 当您希望某些线程等待相当长的时间时(例如,对文件系统进行读/写操作),有时会出现超额预订。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM