线程数量和线程块时间之间的平衡在哪里? [英] Where's the balance between thread amount and thread block times?

查看:254
本文介绍了线程数量和线程块时间之间的平衡在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

细长的问题:

当拥有更多阻塞线程时,CPU核心,线程数量和线程阻塞次数之间的平衡是否通过减少上下文切换开销来最大化CPU效率? / p>

我有各种各样的IO设备,我需要在Windows 7上控制,使用x64多核处理器:PCI设备,网络设备,东西保存到硬驱动器,正在复制的大数据块,...最常见的策略是:在它上面放一个线程!几十个线程以后,这开始觉得是一个坏主意。



我的核心都没有使用100%,还有几个核心仍然闲置,但是有延迟显示在10到100ms的范围内,谁不能解释由IO阻塞或CPU密集使用。其他进程似乎也不需要资源。我怀疑上下文切换开销。



有很多可能的解决方案:




  • 通过捆绑相同的IO设备来减少线程:这主要用于硬盘驱动器,但也可能用于网络。如果我在一个线程中保存20MB到硬盘驱动器,而在另一个线程10MB,不会更好,将它所有相同?

  • 通过捆绑类似的IO设备来减少线程,并提高其优先级:增加优先级的几十个线程可能会使我的用户界面线程断线。



任何处理类似问题的案例研究都可以用来解决类似的问题非常感谢。

解决方案

首先,听起来这些任务应该使用异步I / O(IO完成端口, ,而不是与单独的线程。阻塞线程通常是错误的I / O方式。



其次,阻塞的线程不应影响上下文切换。调度器必须处理所有的活动线程,因此,有很多线程运行(未阻塞)可能会减缓上下文切换。但是,只要大多数线程被阻塞,它们不应该影响那些不是。


Elongated question:
When having more blocking threads then CPU cores, where's the balance between thread amount and thread block times to maximize CPU efficiency by reducing context switch overhead?

I have a wide variety of IO devices that I need to control on Windows 7, with a x64 multi-core processor: PCI devices, network devices, stuff being saved to hard drives, big chunks of data being copied,... The most common policy is: "Put a thread on it!". Several dozen threads later, this is starting to feel like a bad idea.

None of my cores are being used 100%, and there's several cores who're still idling, but there are delays showing up in the range of 10 to 100ms who cannot be explained by IO blockage or CPU intensive usage. Other processes don't seem to require resources either. I'm suspecting context switch overhead.

There's a bunch of possible solutions I have:

  • Reduce threads by bundling the same IO devices: This mainly goes for the hard drive, but maybe for the network as well. If I'm saving 20MB to the hard drive in one thread, and 10MB in the other, wouldn't it be better to post it all to the same? How would this work in case of multiple hard drives?
  • Reduce threads by bundling similar IO devices, and increase it's priority: Dozens of threads with increased priority are probably gonna make my user interface thread stutter. But I can bundle all that functionality together in 1 or a couple of threads and increase it's priority.

Any case studies tackling similar problems are much appreciated.

解决方案

First, it sounds like these tasks should be performed using asynchronous I/O (IO Completion Ports, preferably), rather than with separate threads. Blocking threads are generally the wrong way to do I/O.

Second, blocked threads shouldn't affect context switching. The scheduler has to juggle all the active threads, and so, having a lot of threads running (not blocked) might slow down context switching a bit. But as long as most of your threads are blocked, they shouldn't affect the ones that aren't.

这篇关于线程数量和线程块时间之间的平衡在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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