多线程对 IO 绑定操作有意义吗? [英] Does multithreading make sense for IO-bound operations?

查看:29
本文介绍了多线程对 IO 绑定操作有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行许多磁盘操作时,多线程是帮助、阻碍还是没有区别?

When performing many disk operations, does multithreading help, hinder, or make no difference?

例如,将多个文件从一个文件夹复制到另一个文件夹时.

For example, when copying many files from one folder to another.

澄清:据我所知,当执行其他操作时,并发显然会有所作为.如果任务是打开一个图像文件,转换为另一种格式,然后保存,磁盘操作可以与图像处理同时执行.我的问题是当唯一执行的操作是磁盘操作时,并发排队和响应磁盘操作是否更好.

Clarification: I understand that when other operations are performed, concurrency will obviously make a difference. If the task was to open an image file, convert to another format, and then save, disk operations can be performed concurrently with the image manipulation. My question is when the only operations performed are disk operations, whether concurrently queuing and responding to disk operations is better.

推荐答案

到目前为止,大多数答案都与操作系统调度程序有关.但是,我认为还有一个更重要的因素会导致您的答案.您是写入单个物理磁盘还是多个物理磁盘?

Most of the answers so far have had to do with the OS scheduler. However, there is a more important factor that I think would lead to your answer. Are you writing to a single physical disk, or multiple physical disks?

即使您与多个线程并行...IO 到单个物理磁盘本质上是一个序列化操作.每个线程都必须阻塞,等待机会访问磁盘.在这种情况下,多线程可能没用……甚至可能导致争用问题.

Even if you parallelize with multiple threads...IO to a single physical disk is intrinsically a serialized operation. Each thread would have to block, waiting for its chance to get access to the disk. In this case, multiple threads are probably useless...and may even lead to contention problems.

但是,如果您将多个流写入多个物理磁盘,并发处理它们应该可以提高性能.对于托管磁盘(例如 RAID 阵列、SAN 设备等)而言尤其如此.

However, if you are writing multiple streams to multiple physical disks, processing them concurrently should give you a boost in performance. This is particularly true with managed disks, like RAID arrays, SAN devices, etc.

我认为这个问题与操作系统调度程序没有太大关系,因为它更多地与您写入的磁盘的物理方面有关.

I don't think the issue has much to do with the OS scheduler as it has more to do with the physical aspects of the disk(s) your writing to.

这篇关于多线程对 IO 绑定操作有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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