是否使用并发或并行或两者兼而有之

是否使用并发或并行或两者兼而有之

本文介绍了.NET 是否使用并发或并行或两者兼而有之?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道并行和并发之间的区别.并发意味着处理器以非常快的速度在线程之间切换,这使得它看起来像是并行运行,因此即使使用单核处理器,您也可以同时实现事物.而并行意味着每个线程都运行在不同的内核上.

I know the difference between parallelism and concurrency. Concurrency means that the processor is switching between threads at a very fast rate which makes it look like it's running in parallel, so even with a singlecore processor you can achieve things happening at the same time. While parallelism means that each thread is run on a different core.

.NET 使用并行还是并发?什么是流量?cpu 是否检查线程数是否多于内核数,如果是这种情况,是否会导致内核具有多个线程,从而使程序并行和并发?

Does .NET use parallelism or concurreny? What is the flow? Does the cpu check if there are more threads than cores and if thats the case it will result in a core having multiple threads which makes the program parallel and concurrent?

如果我使用任务并行库会发生什么?这个名字在里面是平行的,那么它是平行的还是两者的结合?

And what happens if I make use of the task parallel library? The name got parallel in it, so is it parallel or the combination of both or?

这个主题对我来说不是很清楚,我一直在怀疑,不知道这一切是如何运作的.

This subject is not very clear to me, I am constantly in doubt and not sure how all of this works.

推荐答案

你对并发和并行的定义有点奇怪.更好的定义可能来自 如何阐明两者之间的区别异步和并行编程

Your definition from concurrent vs parallel is somewhat odd. A better definition might be from How to articulate the difference between asynchronous and parallel programming

当您异步运行某事时,这意味着它是非阻塞的,您无需等待它完成即可执行它并继续执行其他操作.并行意味着同时并行运行多个事物.当您可以将任务分成独立的工作部分时,并行性工作得很好.

您可以与 .Net 并行运行,最简单的方法是使用 Parallel.For.

You can run things in parallel with .Net, the simplest way is to use Parallel.For.

.Net 还允许使用异步函数..Net API 中的大多数异步函数应该映射到操作系统中的异步函数,(在 Windows 上这有时被称为重叠 IO).这允许处理器在等待慢速磁盘或网络数据包时做其他事情.使用异步函数的现代方法是 async/等待.

.Net also allows using asynchronous functions. Most asynchronous functions in the .Net API should be mapped to a asynchronous function in the OS, (on windows this is sometimes referred to as overlapped IO). This allows the processor to do something else while waiting for a slow disk or network packet. The modern way to use asynchronous functions is with async/await.

在使用并行或异步编程时,不能保证任何东西都会并发或以任何特定顺序运行,这取决于操作系统和/或框架.在大多数情况下,这将运行与逻辑处理器一样多的线程.我建议阅读 并行处理、并发和异步编程在 .NET 中 以了解该主题的介绍.

There are no guarantees that anything will run concurrently or in any specific order when using parallel or async programming, that is up to the operating system and/or framework. In most cases this will run as many threads as there are logical processors. I would recommend reading Parallel Processing, Concurrency, and Async Programming in .NET for an introduction to the topic.

这篇关于.NET 是否使用并发或并行或两者兼而有之?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:56