线程性能 [英] Threading Performance

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

问题描述

所以我一直在为我的游戏演示添加一些多线程。这一切都运行良好,直到我创建了比我的CPU上的内核更多的线程。



我只有一个双核,所以我有主线程和额外的辅助线程,如果由于某种原因我创建了第三个线程,那么性能会大幅下降 - 比如从超级跑车到你爸爸的第一个福特。



单线程我获得50 fps,两个线程我获得70 fps但有三个线程我得到8 fps。



使用Very Sleepy看看发生了什么;当我添加第三个线程时花费了很多时间在 WaitForSingleObject ReleaseSemaphore ,但是我只有一个地方使用它,并且无论线程​​数量如何,锁都将被调用相同的次数,因为只有有限的数据量。



我创建线程在启动时使用 CreateThread 然后他们等待en事件发出信号表明他们有工作要做,一旦工作完成,他们将再次等待事件。



我没有做太多的线程,所以我做了一些非常愚蠢的事情,导致了可怕的表现吗?

解决方案

如果你已经在100%cpu运行,拥有比核心更多的线程将减慢你的速度。但是,我编写的程序在双核cpu上运行了几十个线程而没有问题。只需要避免死锁和竞争条件。


当没有给出实际的代码示例时,更难以查看实际问题,但根据您的信息我得到以下内容:

因为在数据有限的情况下你有比核心更多的线程,你有很多开销导致处理器停止。



这在现实生活中的例子更清晰:

手工做菜时,你有一个人洗衣服做干燥。当有一个额外的人(线程)必须竞争以锁定唯一可用的刷子(和接收器)时,很明显,如果没有实际的性能提升,这将变得混乱。以这种方式做菜肯定不会更快。



但是如何加快速度呢?好吧,如果在干燥一件物品后你必须从厨房走到起居室才能将物品拿走,这可能有助于将干燥布留在水槽上,让另一个人(线程)拿走那个资源并使用它另一个人正在放置一件物品(意味着干燥布上的锁被释放)。



这里的结论只是在一个案例中使用比核心更多的线程涉及I / O操作的地方。否则那个时间花在等待上。当线程不得不在已经可用的资源上竞争时,它将不会加速任何事情。在这种情况下,您正在使处理器变得疯狂,因为线程被挂起/从主内存恢复,这意味着您正在颠覆缓存。由此产生的延迟是巨大的,因为计算机程序的性能只是系统执行它所遇到的最大延迟。通过添加一个线程,您只需添加更多延迟密集型操作,从而大幅降低性能。



祝你好运!


看看



如何确定理想的线程数? [ ^ ]

So I've been adding in some multi threading to my game demo. It all works fine until I create more threads than I have cores on my CPU.

I've only got a dual core so I have the main thread and an extra helper thread, if for whatever reason I create a third thread then performance drops considerably - like going from a super car to your dad's first Ford.

With a single thread I get 50 fps, with two threads I get 70 fps but with three threads I get 8 fps.

Using Very Sleepy to see what's going on; when I add the third thread a lot of time is spent in WaitForSingleObject and ReleaseSemaphore, but there is only one place I use that and the locks are going to get called the same number of times regardless of the number of threads because there's only a limited amount of data.

I create the threads at startup using CreateThread and they then wait for en event to signal that there is work for them to do, once the work is done they'll be waiting for the event again.

I've not done much threading so am I doing something immensely stupid that's causing the horrific performance?

解决方案

Having more threads than cores will slow you down if you are already running at 100% cpu. However, I have written programs running dozens of threads on a dual core cpu without a problem. Just need to avoid deadlock and race conditions.


It's harder to look at the actual problem when no actual code example is given, but based on your information I get to the following:
Because you have more threads than cores in a situation with limited data, you have a lot of overhead that causes the processor to stall.

This is way more clear in a real life example:
When doing dishes by hand, you have one person doing the washing and one doing the drying. When there is an extra person (thread) that has to compete to get a lock on the only brush (and sink) available, it is clear that this will get messy without an actual performance gain. Doing dishes this way certainly won't get any faster.

But how to speed it up? Well, if after drying an item you would have to walk from the kitchen to the living room to put that item away, it could help to leave the drying cloth at the sink and let another person (thread) take that resource and use it while the other person is putting an item away (meaning the lock on the drying cloth is released).

The conclusion here is only to use more threads than cores in a case where I/O operations are involved. That time is otherwise spent on waiting. When threads would otherwise have to compete on resources that are already available it won't speed up anything. In that cases you are making the processor crazy because threads are suspended to/restored from main memory, meaning you are thrashing the cache. The latency on that is enormous because the performance of a computer program is simply the maximum latency that the system encounters by executing it. By adding a thread you just added more latency intensive operations which drops performance drastically.

Good luck!


Have a look at the

How to decide ideal number of threads ?[^]


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

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