Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in .NET between developing a multithreaded application and parallel programming?

Recently I've read a lot about parallel programming in .NET but I am still confused by contradicting statements over the texts on this subject.

For example, tThe popup (upon pointing a mouse on tag's icon) description of the stackoverflow.com task-parallel-library tag:

"The Task Parallel Library is part of .NET 4. It is a set of APIs tpo enable developers to program multi-core shared memory processors"

Does this mean that multi-core-d and parallel programming applications impossible using prior versions of .NET?

Do I control a multicore/parallel usage/ditribution between cores in .NET multithreaded application?

How can I identify a core on which a thread to be run and attribute a thread to a specific core?

What has the .NET 4.0+ Task Parallel Library enabled that was impossible to do in previous versions of .NET?

Update:
Well, it was difficult to formulate specific questions but I'd like to better understand:

What is the difference in .NET between developing a multi-threaded application and parallel programming?

So far, I could not grasp the difference between them

Update2:
MSDN "Parallel Programming in the .NET Framework" starts from version .NET 4.0 and its article Task Parallel Library tells:

"Starting with the .NET Framework 4, the TPL is the preferred way to write multithreaded and parallel code"

Can you give me hints how to specifically create parallel code in pre-.NET4 (in .NET3.5), taking into account that I am familiar with multi-threading development?

like image 883
Fulproof Avatar asked Mar 11 '13 05:03

Fulproof


People also ask

What is the difference between multithreading and parallel processing?

Parallel programming is a broad concept. It can describe many types of processes running on the same machine or on different machines. Multithreading specifically refers to the concurrent execution of more than one sequential set (thread) of instructions.

What is difference between multithreading and asynchronous programming C#?

Asynchronous programming is about the asynchronous sequence of Tasks, while multithreading is about multiple threads running in parallel. Multithreading is a way of asynchrony in programming but we can also have single-threaded asynchronous tasks.

Is .NET multi threaded?

With . NET, you can write applications that perform multiple operations at the same time. Operations with the potential of holding up other operations can execute on separate threads, a process known as multithreading or free threading.

Is the preferred way to write multithreaded and parallel code?

In . NET Framework 4, the TPL is the preferred way to write multithreaded and parallel code. However, not all code is suitable for parallelization.


3 Answers

I see "multithreading" as just what the term says: using multiple threads.

"Parallel processing" would be: splitting up a group of work among multiple threads so the work can be processed in parallel.

Thus, parallel processing is a special case of multithreading.


Does this mean that multi-core-d and parallel programming applications impossible using prior versions of .NET?

Not at all. You could do it using the Thread class. It was just much harder to write, and much much harder to get it right.

Do I control a multicore/parallel usage/ditribution between cores in .NET multithreaded application?

Not really, but you don't need to. You can mess around with processor affinity for your application, but at the .NET level that's hardly ever a winning strategy.

The Task Parallel Library includes a "partitioner" concept that can be used to control the distribution of work, which is a better solution that controlling the distribution of threads over cores.

How can I identify a core on which a thread to be run and attribute a thread to a specific core?

You're not supposed to do this. A .NET thread doesn't necessarily correspond with an OS thread; you're at a higher level of abstraction than that. Now, the default .NET host does map threads 1-to-1, so if you want to depend on an undocumented implementation detail, then you can poke through the abstraction and use P/invoke to determine/drive your processor affinity. But as noted above, it's not useful.

What has the .NET 4.0+ Task Parallel Library enabled that was impossible to do in previous versions of .NET?

Nothing. But it sure has made parallel processing (and multithreading) much easier!

Can you give me hints how to specifically create parallel code in pre-.NET4 (in .NET3.5), taking into account that I am familiar with multi-threading development?

First off, there's no reason to develop for that platform. None. .NET 4.5 is already out, and the last version (.NET 4.0) supports all OSes that the next older version (.NET 3.5) did.

But if you really want to, you can do simple parallel processing by spinning up Thread objects or BackgroundWorkers, or by queueing work directly to the thread pool. All of these approaches require more code (particularly around error handling) than the Task type in the TPL.

like image 52
Stephen Cleary Avatar answered Sep 17 '22 01:09

Stephen Cleary


What if i ask you "Do you write business software with your own developed language? or Do you drink water after digging your own well?"

That's the difference in writing multi threading by creating threads and manage them around while you can use abstraction over threads using TPL. Multicore and scheduling of threads on cores is maintained at OS so you don't need to worry about whether your threads are getting executed on the cores your system supports AFAIK.

like image 21
TalentTuner Avatar answered Sep 18 '22 01:09

TalentTuner


Check this article, it basically sums up what was (virtually) impossible before TPL, even though many companies had brewed their own parallel processing libraries none of them had been fully optimized to take advantage of all resources of the popular architectures (simply because it's big task & Microsoft has a lot of resources + they are good). Also it's interesting to note Intel's counterpart implementation TBB vs TPL

like image 29
user1416420 Avatar answered Sep 21 '22 01:09

user1416420