Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize parallel programming in C #

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

C# how to achieve parallel programming, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Parallel related actual combat

When it comes to parallelism, we need to talk about the Task Parallel Library (task parallel library) introduced in NET FX4, referred to as TPL. TPL mainly covers three usage scenarios, data parallelism, task parallelism and pipelining. TPL hides the complex processing in parallel programming with its high encapsulation feature, so that developers can carry out parallel programming with a lower threshold.

Data parallelism

This scenario lies in a large amount of data to be processed and the same operation to be performed on each piece of data.

Task parallelism

If there are many different operations that are relatively independent, or if they can be divided into multiple subtasks but are independent of each other, the advantage of parallelization can be brought into full play through parallel tasks.

Pipeline

Pipeline is a combination of the above two scenarios, which is also the most complex and difficult to deal with, because it involves multiple concurrent tasks to coordinate processing.

This scene, but the editor's understanding is not very good, so do not dare to scribble, multi-party search for information, found an article on oschina.

Pipeline technology refers to allowing computer processing steps to overlap in a machine cycle. In particular, when one instruction is executed, the next instruction can be read, which means that there can be more than one instruction on the "pipeline" at any one time, and each instruction is at a different stage of execution. In this way, even if the time to read and execute each instruction remains the same, the overall throughput of the computer increases.

Original address: https://my.oschina.net/u/3374461/blog/1930305

System.Threading.Tasks.Parallel class

Although the Parallel class is under the System.Threading.Tasks namespace, creating parallel code does not necessarily directly use an instance of the Task class, we can directly use the methods provided by the Parallel static class.

Parallel.For: provides load-balanced parallel execution for a fixed number of independent For loop iterations

Parallel.For (0,5, I = > {Console.WriteLine ("the number is", I);})

Parallel.Foreach: provides load-balanced parallel execution for a fixed number of independent ForEach loop iterations. This method supports a custom divider (Partitioner) to give us complete control over data distribution.

String [] letters = new string [] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"}; Parallel.ForEach (letters, I = > Console.WriteLine ("letter is" + I))

Parallel.Invoke: provides load-balanced parallel execution for a given stand-alone task, which we'll focus on next.

Parallel.Invoke

This method is very practical and simple.

The following code returns the parameterless method of void:

Parallel.Invoke (Method1 (), Method2 (), Method3 (), Method4 ()); run through Lambda expression: Parallel.Invoke (() = > Method1 (), () = > Method2 (), () = > Method3 (), () = > Method4 ()

Run through Lambda expressions and anonymous types:

Parallel.Invoke (() = > {Method1 (); / / Do something}, () = > {Method2 (); / / Do something}, () = > {Method3 (); / / Do something}, () = > {Method4 (); / / Do something})

The above code needs to execute four methods in parallel, but these four methods cannot be executed concurrently if there are fewer than four free logic cores or there are no four logic cores at all. So ideally, when there are at least four free logic cores, we can execute these four methods in parallel.

We cannot accurately predict the execution order of these four methods, because the underlying logic creates the most appropriate execution plan based on the resources available at runtime. Of course, TPL still has a mechanism to ensure the sequential execution of methods, which we will discuss later.

The biggest advantage of Parallel.Invoke is its simplicity, but it can't be used regardless of the situation because it is simple. In fact, we need to weigh the use of it in some scenarios.

If the execution time of these four methods is not consistent, it will take the longest execution time to return control, which may cause some logical kernels to be idle. So we need to predict the approximate execution time, and if it takes too long, we should seriously consider whether we really need to use this method.

Its scalability is poor because it can only call a fixed number of logical kernels, and the rest of the kernel will remain idle.

The interaction between methods is extremely difficult, and it is easy to generate Bug. Of course, this is a common problem in parallel programming, and TPL also takes this into account, and there are sufficient mechanisms to solve this problem.

If one of the methods has an exception, it will be difficult to catch the exception, so you need to write enough logs in the corresponding called method.

The editor also encountered the exception of memory overflow in the previous use, which will also explain the reasons and solutions in future articles.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report