-4

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.

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.

M Yil
  • 877
  • 1
  • 14
  • 35
  • 4
    Where did you read those definitions of parallelism and concurrency? – mjwills Sep 14 '20 at 11:59
  • 1
    How much experience do you have with C#? Have you used async / await? Have you used `Task.Run`? – mjwills Sep 14 '20 at 12:00
  • 1
    This might be interesting: https://stackoverflow.com/a/1050257/10608418 –  Sep 14 '20 at 12:01
  • 1
    Basically parallelism is a subset of concurrency (anything that is parallel is also concurrent but something can be concurrent without being parallel) –  Sep 14 '20 at 12:03
  • @mjwills I have some experience with C# yes. I also know how to work with async/await. But I want to understand truely what is happing under the hood. I also have used Task.Run. It creates a thread to my understanding and the task scheduler does the rest. – M Yil Sep 14 '20 at 12:03
  • @Knoop Allright. So concurrency means doing something at the same time. Parallelism does somethng at the same time, and time-slicing also. Is that true? – M Yil Sep 14 '20 at 12:05
  • That depends on how you define "doing something at the same time", concurrency doesn't require the separate tasks to run at the exact same time (that's the requirement of parallelism). So time-slicing tasks on a single core processor is concurrent but not parallel. However executing 2 tasks on 2 different processor cores at the same time is both concurrent and parallel. –  Sep 14 '20 at 12:12
  • @Knoop thank you. But if I use the task parallel library, there is no guarantee that it will run parallel right even if the library is called parallel? Because we don't know on what processor the thread will run? Or can that be decided in code? – M Yil Sep 14 '20 at 12:15
  • The concept of *concurrency* is not related to processors and cores. You may have multiple concurrent operations, served by [zero threads](https://blog.stephencleary.com/2013/11/there-is-no-thread.html). For example there is zero work to be done by the processor of the local machine, while waiting for the response of a web request. On the other hand *parallelism* is all about processors and cores. Although it's not uncommon to use loosely the term parallelism even when the occurrence of actual parallelism is possible but not guaranteed. – Theodor Zoulias Sep 14 '20 at 13:47

1 Answers1

1

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

When you run something asynchronously it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things. Parallelism means to run multiple things at the same time, in parallel. Parallelism works well when you can separate tasks into independent pieces of work.

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

.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.

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.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Thank you for your help. But there is no guarantee something will run in parallel right? When I create two threads, the scheduler will decide on what processor in will run right? – M Yil Sep 14 '20 at 20:52
  • 1
    @M Yil Yes, it will depend on the scheduler and the machine it will run on. But in most cases the threads will actually run in parallel. But you will need to learn thread safe programming techniques in order to use either concept safely. – JonasH Sep 15 '20 at 06:17