Task Parallel Library in C#

2023-05-16

文章目录

  • Task Parallel Library in C# with Examples (TPL in C#)
    • Introduction to Parallelism
    • What is Parallel Programming in C#?
    • Why do we need Task Parallel Library in C#?
    • What is Parallel Programming in C#?
    • C# Supports Two Types of Parallelism:

Task Parallel Library in C# with Examples (TPL in C#)

In this article, I am going to give you an overview of Parallel Programming and Task Parallel Library in C# with Examples. Please read our previous section articles where we discussed Asynchronous Programming in C#. The Task Parallel Library is also referred to as TPL in C#. At the end of this article, you will understand What Task Parallel Library is and why we need it in C# applications.

在本文中,我将通过示例向您概述c#中的并行编程和任务并行库。请阅读我们之前讨论c#异步编程的部分文章。任务并行库在c#中也被称为TPL。在本文的最后,您将了解什么是任务并行库,以及为什么在c#应用程序中需要它。

Introduction to Parallelism

It is time for us to start talking about parallelism. With parallelism, we can use our processor to perform several actions simultaneously. So, with parallelism, we have the opportunity to improve the speed of certain processes of our programs.

现在是时候开始讨论并行性了。通过并行,我们可以使用处理器同时执行多个操作。因此,通过并行,我们有机会提高程序某些进程的速度。

We will start this module by talking about what parallelism is. Later, we will see different tools for parallelization like Parallel.For, Parallel.Foreach and Parallel.Inoke. We will also talk about when not to use parallelism. We will also see concepts such as Atomic Methods, Thread safety, and race conditions. Then we will see mechanisms to integrate race conditions such as interlocked and locks. Finally, we’ll talk about PLINQ (Parallel LINQ)

我们将从讨论并行是什么开始本模块。稍后,我们将看到用于并行化的不同工具,如Parallel。因为,平行。Foreach和Parallel.Inoke。我们还将讨论何时不使用并行。我们还将了解原子方法、线程安全性和竞争条件等概念。然后,我们将看到集成竞争条件的机制,如互锁和锁。最后,我们将讨论PLINQ(并行LINQ)

What is Parallel Programming in C#?

Parallel Programming in C# helps us divide a task into different parts and work those parts simultaneously. An example might be that we have a set of credit cards and we want to process them simultaneously. Or if we have a set of images and we want to apply a series of filters to each one, we can do this by taking the advantage of parallelism.

c#中的并行编程帮助我们将任务划分为不同的部分,并同时处理这些部分。例如,我们有一组信用卡,我们希望同时处理它们。或者,如果我们有一组图像,我们想对每一张图像应用一系列过滤器,我们可以利用并行性来做到这一点。

The main benefit of parallelism is saving time. Time is saved by maximizing the use of computer resources. The idea is that if the computer allows the use of multi-threading, we can use these threads when we have a task to solve. So, instead of underusing our processor using a single thread, we can use as many threads as we can to speed up the processing of the task.

并行计算的主要优势是节约时间。这是通过最大化计算机资源的使用来实现的。其基本思想是,如果计算机支持多线程,当我们有一个需要解决的任务时,我们可以利用这些线程。因此,我们可以使用尽可能多的线程来加速任务的处理,而不是仅使用单个线程来浪费处理器的性能。

In general, an exception to the benefits of using parallelism using ASP.NET and ASP.NET Core since those scenarios are already parallelized. That is because every thread serves an HTTP request. And therefore, if you have one HTTP request that occupies several threads, then the Web server will have fewer resources to serve other HTTP requests.

一般来说,在使用ASP.NET和ASP.NET Core时,并行计算的优势存在一些例外,因为这些场景已经并行化了。这是因为每个线程都为一个HTTP请求提供服务。因此,如果您有一个占用多个线程的HTTP请求,则Web服务器将没有足够的资源来为其他HTTP请求提供服务。

In general, we use parallelism in CPU-bound operations, CPU bound operations are those operations whose resolution depends on the processor, not on services external to the application. Doing an arithmetic operation will be an example of a CPU-bound operation. Taking a set of images and applying filters and transformations through them will be another CPU-bound operation.

通常情况下,我们在 CPU-bound 操作中使用并行计算。CPU-bound 操作是指其处理时间取决于处理器,而不是取决于应用程序外部的服务。进行算术运算是 CPU-bound 操作的一个例子。将一组图像并对它们应用滤镜和变换也是 CPU-bound 操作的另一个例子。

It is these types of operations, those that are CPU bound, which are candidates for us to use parallelism. It is important to know that parallelism is not always beneficial in terms of performance, since using parallelism has a cost, so we must always make measurements to prove that the cost of parallelism is not greater than not using it. That is sometimes when we use parallelism, the result is a slower application. One reason for this is that when we want to parallelize is very small or doesn’t require enough work for justifying the use of parallelism.

The benefit of parallelism depends on the amount of work to be parallelized. So, Parallel Programming in C# is very important for systems that must process a huge amount of data. For example, on Facebook, approximately two hundred and fifty thousand photos are uploaded per minute. As you can imagine, it takes a lot of power to process such a high volume of information. However, the processors are not getting much faster because of the physical limitations. What is being done then mainly is to include more cores in the processors. In this way, we can take advantage of parallelism to accomplish more tasks in less time.

It is not recommended to occupy several threads for one HTTP request. If you have a long task to do, then it is recommended to use background services or some server technology.

Finally, parallelism shows benefits, if the computer where the program is running has parallelism capabilities. If you try to use parallelism on a computer that cannot use parallelism, then the code will run in sequential. That is, it will not throw any errors, but it will not be faster either. So, the objective is that not all computers can perform parallelism.

Modern processors are typically multicore. In windows, you can see if your computer is multicore by going to task manager, then selecting the performance tab, and having look at the CPU. Here, you can look at the number of available cores and also have a look at the logical processors. In my case, I have 4 cores and 8 logical processors as shown in the below image. This means that each core can do two operations simultaneously.

并行的主要好处是节省时间。通过最大限度地利用计算机资源来节省时间。思想是,如果计算机允许使用多线程,我们可以在解决任务时使用这些线程。因此,我们可以使用尽可能多的线程来加速任务处理,而不是使用单个线程浪费我们的处理器。

一般来说,在使用ASP.NET和ASP.NET Core时,并行使用的好处是一个例外,因为这些情况已经被并行化了。这是因为每个线程都为一个HTTP请求提供服务。因此,如果您有一个HTTP请求占用了多个线程,那么Web服务器将有更少的资源来服务其他HTTP请求。

一般来说,我们在CPU绑定操作中使用并行,CPU绑定操作是那些其解决方案取决于处理器而不是应用程序外部服务的操作。进行算术操作将是CPU绑定操作的示例。获取一组图像并对其应用过滤器和变换将是另一个CPU绑定操作。

这些类型的操作,即CPU绑定操作,是我们使用并行的候选操作。重要的是要知道,并行不总是有利于性能,因为使用并行是有成本的,因此我们必须始终进行测量来证明使用并行的成本不比不使用它更大。有时候,当我们使用并行时,结果是一个更慢的应用程序。一个原因是我们要并行的任务非常小或者不需要足够的工作来证明使用并行的必要性。

并行的好处取决于要并行化的工作量。因此,C#中的并行编程对于必须处理大量数据的系统非常重要。例如,在Facebook上,每分钟大约上传两十五万张照片。正如你所想象的那样,处理如此高的信息量需要大量的计算能力。但是,由于物理限制,处理器并没有变得更快。主要的做法是在处理器中增加更多的核心。通过这种方式,我们可以利用并行来在较短的时间内完成更多的任务。

不建议为一个HTTP请求占用多个线程。如果您有一个长时间的任务要完成,则建议使用后台服务或一些服务器技术。

最后,如果运行程序的计算机具有并行性能,则并行显示出好处。如果您尝试在不能使用并行性的计算机上使用并行性,则代码将按顺序运行。也就是说,它不会抛出任何错误,但也不会更快。因此,目标是并非所有计算机都可以执行并行操作。

现代处理器通常是多核心的。在 Windows 操作系统中,您可以通过打开任务管理器,然后选择性能选项卡来查看您的计算机是否是多核心的。在此处,您可以查看可用的核心数,以及逻辑处理器数。在我的电脑上,我有4个核心和8个逻辑处理器,这意味着每个核心可以同时执行两个操作。

在这里插入图片描述

In C#, we mainly use two tools to work with parallelism. They are as follows:

The Task Parallel Library (TPL)
Parallel LINQ (PLINQ)
The Task Parallel Library is a library that makes life easier for us. When we see parallelism in our programs that TPL (Task Parallel Library) abstracts the low-level detail of thread handling, allowing us to run programs that run parallel without having to work with these threads manually.

On the other hand, PLINQ or Parallel LINQ is an implementation of LINQ that allows us to work in parallel. For example, in LINQ, we can filter the elements of an array. Then with Parallel LINQ, we can filter the same array in parallel. This allows us to use the cores of our processor to perform the evaluations of the elements of the array simultaneously.

Why do we need Task Parallel Library in C#?

We can’t expect our sequential program to run faster on the new processors as we know the processor technology advances means the focus is on Multicore-processors. Today’s desktop typically has 4 cores but the latest experimental multi-core chips have up to 1000 cores.

So in simple words, we can say that the multicore processor machines are now becoming standard and the aim is to improve the performance by running a program on multiple processors in parallel. So by considering the above scenario, the .NET Framework 4 introduces Task Parallel Library (TPL) that makes it easier for developers to write parallel programs that target multi-core machines (automatically use multiple processors) which improves the performance.

Using the Task Parallel Library (TPL), we can express the parallelism in the existing sequential code, which means we can express the code as a Parallel task, which will be run concurrently on all the available processors.
在 C# 中,我们主要使用两个工具来处理并行性,它们是:

任务并行库(TPL)
并行 LINQ(PLINQ)
任务并行库是一个库,它为我们的工作提供了便利。当我们在程序中看到并行性时,TPL(任务并行库)抽象了线程处理的底层细节,允许我们在不必手动处理这些线程的情况下运行并行程序。

另一方面,PLINQ 或并行 LINQ 是 LINQ 的一种实现,它允许我们并行工作。例如,在 LINQ 中,我们可以过滤数组的元素。然后,在 Parallel LINQ 中,我们可以并行过滤同一个数组。这使我们能够使用处理器的核心来同时执行数组元素的评估。

为什么我们需要 C# 中的任务并行库?
我们不能指望我们的顺序程序在新的处理器上运行得更快,因为处理器技术的进步意味着重点在于多核处理器。今天的桌面通常有 4 个核心,但最新的实验性多核芯片具有高达 1000 个核心。

因此,简单来说,我们可以说多核处理器机器现在正在成为标准,目标是通过在多个处理器上并行运行程序来提高性能。因此,.NET Framework 4 引入了任务并行库(TPL),使开发人员能够编写针对多核机器(自动使用多个处理器)的并行程序,从而提高性能。

使用任务并行库(TPL),我们可以在现有的顺序代码中表达并行性,这意味着我们可以将代码表示为并行任务,该任务将在所有可用处理器上同时运行。

在这里插入图片描述

What is Parallel Programming in C#?

Parallel Programming in C# is a type of programming in which many calculations or the execution of processes are carried out simultaneously. The Points to Remember while working with Parallel Programming:

The Tasks must be independent.
The order of the execution does not matter

C# Supports Two Types of Parallelism:

Data Parallelism: In Data Parallelism, we have a collection of values and we want to use the same operation on each of the elements in the collection. The examples will be to filter the elements of an array in parallel or find the inverse of each matrix in a collection. This means each process does the same work on unique and independent pieces of data.
什么是C#中的并行编程?
C#中的并行编程是指同时进行多个计算或进程执行的一种编程类型。在处理并行编程时需要注意以下几点:

任务必须相互独立。
执行顺序无关紧要。
C#支持两种并行方式:
数据并行:在数据并行中,我们有一个值的集合,并且我们希望对集合中的每个元素使用相同的操作。例如,可以并行地过滤数组的元素或在集合中找到每个矩阵的逆。这意味着每个进程在独立的数据片上执行相同的任务。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Task Parallel Library in C# 的相关文章

随机推荐

  • 查看ubuntu版本号

    转自 xff1a http www cnblogs com zero1665 archive 2010 05 24 1742962 html 方法一 xff1a cat etc issue 返回结果 xff1a Ubuntu 6 06 2
  • Android SDK聚合原理讲解(参考U8)

    想要实现一套聚合sdk框架 xff0c 我们来思考一下 xff0c 我们接入一个sdk xff0c 需要实现哪些东西 1 首先 xff0c 客户端需要接入多款SDK xff0c 为了能够多款游戏重用 xff0c 我们不可以在游戏里面直接去接
  • 高德地图POI数据2020年高德POI

    高德地图POI是Point of Interest 的缩写 xff0c 可以翻译成兴趣点 xff0c 一共有三级分类 xff08 大类 中类 小类 xff09 xff0c 其中一级分类有23个 涵盖餐饮服务 购物服务 生活服务 体育休闲服务
  • C++笔试题整理

    目录 1 笔试题1 1 1 链表反转 1 2 String 2 笔试题2 2 1 求下面函数的返回值 xff08 微软 xff09 2 2 什么是 引用 xff1f 申明和使用 引用 要注意哪些问题 xff1f 2 3 将 引用 作为函数参
  • C/C++程序实现通过http代理访问网页内容

    公司通过代理上网 xff0c C程序直接通过发http请求不能获取网页内容 xff0c 故实现了下通过代理访问http网页的一个测试程序 程序很简单 xff0c 有几个重点 先通过socket直接连接代理服务器 向代理服务器发送HTTP的C
  • Ubuntu搭建Apache+Svn+Submin环境

    1 环境搭建部分参考网站 xff1a 搭建Apache 43 Svn 43 Submin环境 2 使用submin最新版2 3 3 不通过邮件方式 xff0c 直接修改admin密码 xff1a vim passwd py 输入脚本如下 s
  • 磁力计如何用来计算姿态(2)

    上一篇 磁力计如何用来计算姿态 xff08 1 xff09 介绍了磁强计算姿态角的原理 本篇介绍 xff0c 在无人飞行器上 常用的 加速度计 43 磁强计 的定姿方法 静止状态 抑或 悬停状态 xff1a 利用加速度 计算横滚角 xff0
  • readdir_r()

    概述 xff1a 1 readdir r函数 xff0c 是readdir函数的可重入版本 xff0c 也就是线程安全的 2 readdir函数使用静态数据 xff0c 因而不可重入 xff0c 即不是线程安全的 readdir r 就是采
  • CSDN>>2011年重大IT安全事件回顾

    在2010年新年前夜 xff0c 由于无人知道密码 xff0c 旧金山无法获取其紧急行动中心上运行的备份系统 xff0c 这或许可以被视为一个不祥的预兆 2011年即将过去 xff0c 我们需要回顾并盘点一下这一年中发生的重大IT安全事件
  • FRP入门篇

    目录 一 前言 1 概述 2 原理 3 支持功能 4 适用场景 二 环境准备 三 使用 1 安装包下载 2 服务端部署 2 1 上传安装包 2 3 启动服务端 3 客户端部署 3 1 代理服务准备 3 2 上传安装包 3 3 客户端配置 3
  • Python 笔记(14)— 类对象及属性内置方法 classmethod、delattr、dir、hasattr、getattr、callable

    1 classmethod Python 使用关键字 class 定制自己的类 xff0c self 表示类实例对象本身 classmethod 修饰符对应的函数不需要实例化 xff0c 不需要 self 参数 第一个参数需要是表示自身类的
  • Depends, python2.7-minimal (= 2.7.15_rc1-1) 问题解决方法使用 aptitude 安装以及与 apt-get 的区别

    1 问题现象 使用命令 sudo apt install python2 7 安装 Python2 时报如下错误 wohu 64 ubuntu1804 etc apt span class token function sudo span
  • Ubuntu 安装 Docker - Dependency failed for Docker Application Container

    Docker 安装 由于 apt 官方库里的 Docker 版本可能比较旧 xff0c 所以先卸载可能存在的旧版本 xff1a span class token function sudo span span class token fun
  • Docker 入门系列(2)- Docker 镜像、获取查看、修改镜像标签、查找删除创建镜像、导入导出镜像

    Docker 运行容器前需要本地存在对应的镜像 xff0c 如果镜像没保存在本地 xff0c Docker 会尝试先从默认镜像仓库下载 https hub docker com xff0c 用户也可以通过配置 xff0c 使用自定义的镜像仓
  • FreeRTOS事件组之事件组等待位(xEventGroupWaitBits)

    1 函数说明 xff08 xEventGroupWaitBits xff09 函数原型 xff1a EventBits t xEventGroupWaitBits const EventGroupHandle t xEventGroup c
  • try catch里面try catch嵌套

    try catch里能否内嵌try catch xff1f 答案是肯定的 但是等内层try catch出异常之后是个什么执行顺序呢 xff1f 看下面代码 static void Main string args try Console W
  • MFC(VC2010升级到VC2019)

    1 SDKDDKVer h No such file or directory 解决 xff1a 项目 属性 配置属性 常规 windowsSDK版本默认的时8 1 xff0c 但是windows10得更改 xff0c 点击右边下拉按钮 x
  • c# treeview单击树节点,两个节点来回切换闪烁

    使用AfterSelect可以解决这个问题 修改前事件 修改后事件
  • ROS2简介

    转载地址 ROS简介 ROS是Robot Operating System的缩写 xff0c 是一套开源的软件框架和工具集 xff0c 用来帮助开发人员建立机器人应用程序 xff0c 它提供了硬件抽象 设备驱动 函数库 可视化工具 消息传递
  • Task Parallel Library in C#

    文章目录 Task Parallel Library in C with Examples TPL in C Introduction to ParallelismWhat is Parallel Programming in C Why