首页 \ 问答 \ 非阻塞I / O是否比多线程阻塞I / O更快?(Is non-blocking I/O really faster than multi-threaded blocking I/O? How?)

非阻塞I / O是否比多线程阻塞I / O更快?(Is non-blocking I/O really faster than multi-threaded blocking I/O? How?)

我在网络上搜索了关于阻塞I / O和非阻塞I / O的一些技术细节,我发现几个人表示非阻塞I / O将比阻塞I / O更快。 例如在本文档中 。

如果我使用阻塞I / O,那么当然被阻止的线程不能做任何事情...因为它被阻止。 但是一旦线程开始被阻塞,操作系统就可以切换到另一个线程,而不是切换回来,直到有被阻塞的线程要做。 所以只要系统上有另一个线程需要CPU并且不被阻止,与基于事件的非阻塞方法相比,不应该有更多的CPU空闲时间,是吗?

除了减少CPU空闲时间外,还有一个选项可以增加计算机在给定时间范围内执行的任务数量:减少切换线程引入的开销。 但是怎么做呢? 是足够大的足够显示可衡量的效果的头顶吗? 这是一个关于如何使图片工作的想法:

  1. 要加载文件的内容,应用程序将此任务委托给基于事件的i / o框架,将回调函数与文件名一起传递
  2. 事件框架委托给操作系统,该操作系统对硬盘的DMA控制器进行编程,直接将文件写入内存
  3. 事件框架允许进一步的代码运行。
  4. 完成磁盘到内存复制后,DMA控制器将导致中断。
  5. 操作系统的中断处理程序通知基于事件的i / o框架关于完全加载到内存中的文件。 怎么做呢 使用信号?
  6. 事件i / o框架中当前运行的代码完成。
  7. 基于事件的i / o框架检查其队列,并从步骤5看到操作系统的消息,并执行步骤1中获得的回调。

这是怎么运作的? 如果没有,它是如何工作的? 这意味着事件系统可以工作,而不需要明确地触摸堆栈(例如需要备份堆栈的真正的调度程序,并且在切换线程时将另一个线程的堆栈复制到内存中)? 这实际上节省了多少时间? 还有更多吗?


I searched the web on some technical details about blocking I/O and non blocking I/O and I found several people stating that non-blocking I/O would be faster than blocking I/O. For example in this document.

If I use blocking I/O, then of course the thread that is currently blocked can't do anything else... Because it's blocked. But as soon as a thread starts being blocked, the OS can switch to another thread and not switch back until there is something to do for the blocked thread. So as long as there is another thread on the system that needs CPU and is not blocked, there should not be any more CPU idle time compared to an event based non-blocking approach, is there?

Besides reducing the time the CPU is idle I see one more option to increase the number of tasks a computer can perform in a given time frame: Reduce the overhead introduced by switching threads. But how can this be done? And is the overhead large enough to show measurable effects? Here is an idea on how I can picture it working:

  1. To load the contents of a file, an application delegates this task to an event-based i/o framework, passing a callback function along with a filename
  2. The event framework delegates to the operating system, which programs a DMA controller of the hard disk to write the file directly to memory
  3. The event framework allows further code to run.
  4. Upon completion of the disk-to-memory copy, the DMA controller causes an interrupt.
  5. The operating system's interrupt handler notifies the event-based i/o framework about the file being completely loaded into memory. How does it do that? Using a signal??
  6. The code that is currently run within the event i/o framework finishes.
  7. The event-based i/o framework checks its queue and sees the operating system's message from step 5 and executes the callback it got in step 1.

Is that how it works? If it does not, how does it work? That means that the event system can work without ever having the need to explicitly touch the stack (such as a real scheduler that would need to backup the stack and copy the stack of another thread into memory while switching threads)? How much time does this actually save? Is there more to it?


原文:https://stackoverflow.com/questions/8546273
更新时间:2024-01-23 10:01

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。