19

It appears that the Task class provides us the ability to use multiple processors of the system. Does the Thread class work on multiple processors as well or does it use time slicing only on a single processor? (Assuming a system with multiple cores).

My question is if threads will/could be executed on multiple cores then what is so special about Task and Parallelism ?

CriketerOnSO
  • 2,600
  • 2
  • 15
  • 24
  • 1
    This is probably O/S dependant and not determined by the .Net framework. Would be good to know for sure. – Polyfun Oct 03 '14 at 15:14
  • 2
    Keep in mind, Tasks by default are meant for really quick tasks (less than 200ms); so compute-bound parallelism/concurrency shouldn't be a big requirement. If you *do* want to have compute-bound parallel/concurrent, long-running tasks, prefer Task.Run over TaskFactory.StartNew and be sue to use an overload that accepts `TaskCreationOptions` and use `TaskCreationOptions.LongRunning`. Otherwise there may be adverse affects on the thread pool that may defeat the purpose of concurrency. – Peter Ritchie Oct 03 '14 at 21:52
  • Possibly related: http://stackoverflow.com/q/12328751 – stakx - no longer contributing Oct 04 '14 at 07:44

3 Answers3

17

When you create a thread it forms kind of a logical group of work. The .NET Framework will aquire CPU-Time from the system. Most likely multiple threads will run on different cores (This is something the system handeles - not even .NET has any influence on this)

But it might be possible that the system will execute all your Threads on the same core or even moves the execution between several cores during the execution. Keep in Mind, that you are crating managed Threads, and not real System-Threads.

(Correctly spoken I should say: The System could execute your managed Threads within the same System-Thread or use multiple System-Threads for multiple managed threads.)

Maybe you want to have a look at this Blog-Post: http://www.drdobbs.com/parallel/managed-threads-are-different-from-windo/228800359 The explanation there is pretty good in terms of details.

dognose
  • 20,360
  • 9
  • 61
  • 107
  • 2
    Thank you for the answers, if threads will/could be executed on multiple cores then what is so special about Task Parallelism ? – CriketerOnSO Oct 03 '14 at 15:40
  • Maybe that was a little bad wording: It will not run on multiple cores at the same time. But it could be moved from "core to core" as the system decides to do so. (It can also be suspended meanwhile to priorize other threads etc...) – dognose Oct 03 '14 at 15:42
14

Not a bad first question. +1

I would suggest you to read Threading in C# by Joseph Albahari. If you read through the post you will find:

How Threading Works

Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. A thread scheduler ensures all active threads are allocated appropriate execution time, and that threads that are waiting or blocked (for instance, on an exclusive lock or on user input) do not consume CPU time.

So multi threading is handled by operating system through a thread scheduler.

Further the post has:

On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency, where different threads run code simultaneously on different CPUs. It’s almost certain there will still be some time-slicing, because of the operating system’s need to service its own threads — as well as those of other applications.

user2711965
  • 1,795
  • 2
  • 14
  • 34
  • 2
    Your "Not a bad first question. +1" remark should probably be moved into a comment on the question, as it won't be relevant or helpful to other users who find this question. – Spooky Oct 04 '14 at 02:31
11

-It appears that Task class provide us the ability to use on multiple processors in the system.

-if threads will/could be executed on multiple cores then what is so special about Task Parallelism ?

The Task class is just a small, but important, part of TPL (Task Parallel Library). TPL is a high level abstraction, so you don't have to work with threads directly. It encapsulates and hides most of the churn you'd have to implement for any decent multi-threaded application.

Tasks don't introduce any new functionality that you couldn't implement on your own, per se (which is the core of your questions, I believe). They can be synchronous or asynchronous - when they are async, they're either using Thread class internally or IOCP ports.

Some of the points addressed by TPL are:

  • Rethrow exceptions from a child thread on the calling thread.
  • Asynchronous code (launch thread -> run arbitrary code while waiting for child thread -> resume when child thread is over) looks as if it were synchronous, greatly improving readability and maintainability
  • Simpler thread cancelation (using CancellationTokenSource)
  • Parallel queries/data manipulation using PLINQ or the Parallel class
  • Asynchronous workflows using TPL Dataflow
Community
  • 1
  • 1
dcastro
  • 66,540
  • 21
  • 145
  • 155
  • 1
    Thanks a lot for the answer and addressing the core issue. I believe I wasn't clear enough in my question. So there is no difference with respect to parallelism with respect to TPL and a classic thread. Since both will internally use time slicing or multiple cores based on OS, right ? – CriketerOnSO Oct 03 '14 at 15:54
  • @CriketerOnSO Correct :) TPL is a set of libraries partially built on top of the existing `Thread` class. – dcastro Oct 03 '14 at 15:55