哪个更占用CPU资源:异步读取还是同步读取? [英] Which is more CPU intensive: Asynchronous reads or Synchronous reads?

查看:218
本文介绍了哪个更占用CPU资源:异步读取还是同步读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从通信端口进行的异步读取和同步读取之间的CPU使用率是否存在差异?

我曾尝试将读取操作放在一个单独的线程中,但没有发现任何区别.

Is there a difference in CPU usage between Asynchronous and Synchronous reads from a communication port?

I''ve tried putting the reads in a separate thread but I don''t see any difference.

推荐答案

这取决于您的工作.异步读取肯定会使用更多资源.但是请考虑如果使用同步读取会发生什么.如果所有代码都是顺序执行的,并且在两者之间进行了同步读取,则可以节省一些时间.这对于某些控制台应用程序来说很典型.

当您需要一些UI应用程序时,您需要保持UI响应能力.由于同步操作正在阻塞,因此您需要在其他线程中执行此操作,而不是在UI线程中执行.因此,您需要花费一些CPU时间来支持线程,并且基本上要达到与异步读取相同的情况.

现在,不要误会我的意思:我不促进异步操作.恰恰相反:我建议任何人都严格按照我在上一段中所述的方法进行操作:创建其他线程.与过于复杂的异步操作相比,它们工作得很好,并使代码更直接.

我真的认为,当线程API不再像现在这样普遍时,同步就会蓬勃发展.使用同步"操作本质上与在幕后有一个额外的线程相同,但是没有线程所具有的所有控制权.如果线程做得足够好,则不应使用异步.我总是回避它们;对我来说,它们只是过时了.请考虑一下.

—SA
It depends on what you do. Asynchronous read certainly uses a bit more resources. But consider what happens if you use synchronous read. If all you code is sequential with the synchronous read in between, you really save some time. This is typical for some console application.

When you need some UI application, you need to keep UI responsive. As the synchronous operation is blocking, you need do it in some other thread, not in the UI thread. So, you need to spend some CPU time on support of threading and essentially get to the same situation as with asynchronous read.

Now, don''t get me wrong: I do not promote asynchronous operations. Just the opposite: I advise anyone to do exactly what I described in the above paragraph: to create additional thread(s). They work just fine, and make the code more straightforward compared to somewhat over-complicated asynchronous operations.

I really think that the аsynchronous flourished when threading APIs were not common place as they are now. Using аsynchronous operations is essentially the same as having an extra thread behind the scene, but without all the control you have with the threads. If you do threading well enough, you should not use asynchronous. I always avoided them; to me, they are just obsolete. Please think about it.

—SA


这篇关于哪个更占用CPU资源:异步读取还是同步读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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