locked
.NET Tasks Vs Threads : Can Threads take advantage of multi-core processors ?? RRS feed

  • Question

  • HI All,

    Can "threads" take advantage of Multi Core processors OR only tasks can take advantage of multi-core processors, because of the scheduler/CLR Threadpool ?

    Say i have an application which was written in .NET 3.5 (where there was no tasks concept), using multiple threads. Now i am compiling the same in .NET 4.0. Say the servers are also multi-core machines. Now my questions are :

    1) Will the multi threaded application(which is not creating threads using thread-pool) can take advantage of multi-core processors ? Or all the threads run on the same core ?

    2) Are we recommending developers to move to Tasks, because when we create multiple threads it's the overhead of context switching only, when we create more number of threads than the number of cores ? (apart from work-stealing algorithm in Tasks) 

    3) If the max of threads in my application are less than (No-Of-Cores/2) only, In that case can i still use Threads instead of Tasks ? (leave work-stealing from the picture for now)

    Please advise ?

    Thanks,

    Sudheer


    SudheerReddy Esireddy

    Monday, April 29, 2013 1:48 AM

Answers

  • Tasks in .Net 4.0 are pretty much just a simpler way to work with Threads. There is nothing special about Tasks that would make them more multi-core friendly than Threads. Tasks are multi-core friendly because different Tasks can execute on different Threads.

    It's recommended to move to Tasks, because they are easier to use and because they are more efficient (since they use ThreadPool).

    • Marked as answer by SudheerReddy Thursday, May 2, 2013 6:19 AM
    Monday, April 29, 2013 12:42 PM
  • Threads take advantage of Multi Core processors.

    1) The OS will distribute threads across the cores as needed. You can use the Concurrency Visualizer to see this happen. You have to go out of your way to configure threads to run on the same core.

    2) The reasons for that recommendation would be to use the ThreadPool and not create dedicated threads.

    3) You can always use Threads, instead of Tasks. I personally would say that there is rarely (next to never) a reason to create a Thread itself. It's a lot better to either submit work items to the ThreadPool, or to create Tasks. Not only is it more resource efficient to do so (and scalable), but should an exception occur on the other thread it's easier to get the exception back to your calling thread with either EndInvoke (for ThreadPool work items) or .Wait/.Result (for Tasks).

    • Marked as answer by SudheerReddy Thursday, May 2, 2013 6:06 AM
    Monday, April 29, 2013 4:25 PM

All replies

  • Tasks in .Net 4.0 are pretty much just a simpler way to work with Threads. There is nothing special about Tasks that would make them more multi-core friendly than Threads. Tasks are multi-core friendly because different Tasks can execute on different Threads.

    It's recommended to move to Tasks, because they are easier to use and because they are more efficient (since they use ThreadPool).

    • Marked as answer by SudheerReddy Thursday, May 2, 2013 6:19 AM
    Monday, April 29, 2013 12:42 PM
  • Threads take advantage of Multi Core processors.

    1) The OS will distribute threads across the cores as needed. You can use the Concurrency Visualizer to see this happen. You have to go out of your way to configure threads to run on the same core.

    2) The reasons for that recommendation would be to use the ThreadPool and not create dedicated threads.

    3) You can always use Threads, instead of Tasks. I personally would say that there is rarely (next to never) a reason to create a Thread itself. It's a lot better to either submit work items to the ThreadPool, or to create Tasks. Not only is it more resource efficient to do so (and scalable), but should an exception occur on the other thread it's easier to get the exception back to your calling thread with either EndInvoke (for ThreadPool work items) or .Wait/.Result (for Tasks).

    • Marked as answer by SudheerReddy Thursday, May 2, 2013 6:06 AM
    Monday, April 29, 2013 4:25 PM
  • Hi Jared/svick,

          Thanks a lot for the response. I agree to the point that Tasks are more core-friendly and they can dynamically scale-up/down based on the number of cores. So as per the discussion i can understand the following 

    1. Threads will be allocated by OS to different cores.
    2. Creating Threads using Thread pool is recommended as thread pool takes care of creating threads based on the availability of resources (cpu/cores etc...)
    3. With .NET 4.0 Creating Tasks is more preferred As it internally takes care of
    • Creating Threads using Thread-pool
    • Takes care of exceptions (aggregating etc)
    • Supporting stopping/waiting/Cancellation for multiple tasks and other stuff.

    Thanks,


    SudheerReddy Esireddy

    Thursday, May 2, 2013 6:19 AM