Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. threading

threading

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionasp-net
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S steve_a_p

    Thank you for the information. The optimal number of threads equals the number of processors on a system and it shouldn't exceed twice the processor count. Correct? Why not 3 times?

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #8

    The main message is it is not tens of threads: just adding more and more threads will increase the overhead and not reduce the overall execution time. And it does depend on the thread function: if it is pure computation, one thread per processor will do; if there is I/O latency involved, two per processor will be better (and more typically does not help due to I/O limitations); if it is a pure Sleep() you can have as many as you want. The good thing is if you organize things around Environment.ProcessorCount: 1. you can try another factor and observe what it brings you. 2. your app is less likely to suddenly behave badly when you move it to a system with a different processor count (which may well happen when you fix the number of threads). And as Patrick pointed out, you are not completely in charge: 1. with .NET you will not (easily) control which thread runs on which processor; 2. other apps and services will be running too.

    Luc Pattyn [Forum Guidelines] [My Articles]


    this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


    S 1 Reply Last reply
    0
    • P Patrick Etc

      Can I just say, this entire discussion in the context of the .NET Framework is a waste of time. The Framework provides no control over where those threads are going to execute, nor do they provide synchronization capabilities across processor cores. The only limit on the number of threads you can actually create is the limit of the addressable memory space on the machine. If you really need a parallelized application, the .NET Framework is not the platform to use. If you need this, you should be using something like MPI, OpenMP, or a newer solution by Intel called Threading Building Blocks (http://osstbb.intel.com/[^]) which is aware not only of separate processors, but distinct processor cores. So far as I am aware, there are no .NET wrappers for those libraries. You'd be using C/C++. Once you get to those libraries, a question of how many threads to use becomes heavily dependent on what each thread is doing and the overall architecture of a parallelized application.


      The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #9

      Yeah, it is not perfect, but my main objective was to warn against too many threads; some people seem to expect performance increases linearly with the number of threads, which obviously is not true at all. So for a single processor I often give the following approach: "as a first approximation, this is what you could do: write and run the code to use a single thread; observe the CPU load (with Task Manager) as a percentage. The inverse number is the number of threads you want, so if it is close to 100% extra threads won't bring you anything; if it is near 25% using four threads MIGHT increase the performance by four, bringing the CPU load to 100%. More threads than that won't help you." And one should not forget the obvious approach to increasing performance, is by optimizing the application at all levels: algorithms, data structures, coding, compiler settings, etc. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


      1 Reply Last reply
      0
      • P Patrick Etc

        Can I just say, this entire discussion in the context of the .NET Framework is a waste of time. The Framework provides no control over where those threads are going to execute, nor do they provide synchronization capabilities across processor cores. The only limit on the number of threads you can actually create is the limit of the addressable memory space on the machine. If you really need a parallelized application, the .NET Framework is not the platform to use. If you need this, you should be using something like MPI, OpenMP, or a newer solution by Intel called Threading Building Blocks (http://osstbb.intel.com/[^]) which is aware not only of separate processors, but distinct processor cores. So far as I am aware, there are no .NET wrappers for those libraries. You'd be using C/C++. Once you get to those libraries, a question of how many threads to use becomes heavily dependent on what each thread is doing and the overall architecture of a parallelized application.


        The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

        S Offline
        S Offline
        steve_a_p
        wrote on last edited by
        #10

        ok, thank you

        1 Reply Last reply
        0
        • L Luc Pattyn

          The main message is it is not tens of threads: just adding more and more threads will increase the overhead and not reduce the overall execution time. And it does depend on the thread function: if it is pure computation, one thread per processor will do; if there is I/O latency involved, two per processor will be better (and more typically does not help due to I/O limitations); if it is a pure Sleep() you can have as many as you want. The good thing is if you organize things around Environment.ProcessorCount: 1. you can try another factor and observe what it brings you. 2. your app is less likely to suddenly behave badly when you move it to a system with a different processor count (which may well happen when you fix the number of threads). And as Patrick pointed out, you are not completely in charge: 1. with .NET you will not (easily) control which thread runs on which processor; 2. other apps and services will be running too.

          Luc Pattyn [Forum Guidelines] [My Articles]


          this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


          S Offline
          S Offline
          steve_a_p
          wrote on last edited by
          #11

          thanks for the answer

          1 Reply Last reply
          0
          • P Patrick Etc

            Can I just say, this entire discussion in the context of the .NET Framework is a waste of time. The Framework provides no control over where those threads are going to execute, nor do they provide synchronization capabilities across processor cores. The only limit on the number of threads you can actually create is the limit of the addressable memory space on the machine. If you really need a parallelized application, the .NET Framework is not the platform to use. If you need this, you should be using something like MPI, OpenMP, or a newer solution by Intel called Threading Building Blocks (http://osstbb.intel.com/[^]) which is aware not only of separate processors, but distinct processor cores. So far as I am aware, there are no .NET wrappers for those libraries. You'd be using C/C++. Once you get to those libraries, a question of how many threads to use becomes heavily dependent on what each thread is doing and the overall architecture of a parallelized application.


            The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

            R Offline
            R Offline
            Robert Rohde
            wrote on last edited by
            #12

            So just because a better threading lib exists in C one shouldn't use threading in .NET? Weird logic. I've used multithreading in dozens of situations in .NET and although it is not "aware" of the number of physical processors/cores it generally does a good job. Why? Because the operating system takes care of it.

            P 1 Reply Last reply
            0
            • R Robert Rohde

              So just because a better threading lib exists in C one shouldn't use threading in .NET? Weird logic. I've used multithreading in dozens of situations in .NET and although it is not "aware" of the number of physical processors/cores it generally does a good job. Why? Because the operating system takes care of it.

              P Offline
              P Offline
              Patrick Etc
              wrote on last edited by
              #13

              Robert Rohde wrote:

              So just because a better threading lib exists in C one shouldn't use threading in .NET? Weird logic. I've used multithreading in dozens of situations in .NET and although it is not "aware" of the number of physical processors/cores it generally does a good job. Why? Because the operating system takes care of it.

              Of course not. I was responding specifically in the context of counting threads based on the number of processors you have. That's pointless in .NET. Yes, the OS will do some thread parallelization for you, but if you actively want to use those processors you have to use lower level languages with specialized libraries. There's nothing wrong with threading, but you have to understand your problem and the limitations of various solutions before you start creating tons of threads thinking it will solve your problem, as Luc accurately points out.


              The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

              R L 2 Replies Last reply
              0
              • P Patrick Etc

                Robert Rohde wrote:

                So just because a better threading lib exists in C one shouldn't use threading in .NET? Weird logic. I've used multithreading in dozens of situations in .NET and although it is not "aware" of the number of physical processors/cores it generally does a good job. Why? Because the operating system takes care of it.

                Of course not. I was responding specifically in the context of counting threads based on the number of processors you have. That's pointless in .NET. Yes, the OS will do some thread parallelization for you, but if you actively want to use those processors you have to use lower level languages with specialized libraries. There's nothing wrong with threading, but you have to understand your problem and the limitations of various solutions before you start creating tons of threads thinking it will solve your problem, as Luc accurately points out.


                The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

                R Offline
                R Offline
                Robert Rohde
                wrote on last edited by
                #14

                Patrick Sears wrote:

                I was responding specifically in the context of counting threads based on the number of processors you have. That's pointless in .NET.

                Not totally. I agree that you cannot be 100% sure that if you run 4 threads in .NET that every thread will run on its own core (although it does it very often). Nevertheless you have a "hint" on how many parellel threads might be good. If you know you have only one core than you would normally not start several threads (assumming its only about number crunching). But I agree with your other points. :) Robert

                P 1 Reply Last reply
                0
                • R Robert Rohde

                  Patrick Sears wrote:

                  I was responding specifically in the context of counting threads based on the number of processors you have. That's pointless in .NET.

                  Not totally. I agree that you cannot be 100% sure that if you run 4 threads in .NET that every thread will run on its own core (although it does it very often). Nevertheless you have a "hint" on how many parellel threads might be good. If you know you have only one core than you would normally not start several threads (assumming its only about number crunching). But I agree with your other points. :) Robert

                  P Offline
                  P Offline
                  Patrick Etc
                  wrote on last edited by
                  #15

                  Robert Rohde wrote:

                  Not totally. I agree that you cannot be 100% sure that if you run 4 threads in .NET that every thread will run on its own core (although it does it very often). Nevertheless you have a "hint" on how many parellel threads might be good. If you know you have only one core than you would normally not start several threads (assumming its only about number crunching).

                  And I also agree with you there ;)


                  The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

                  1 Reply Last reply
                  0
                  • P Patrick Etc

                    Robert Rohde wrote:

                    So just because a better threading lib exists in C one shouldn't use threading in .NET? Weird logic. I've used multithreading in dozens of situations in .NET and although it is not "aware" of the number of physical processors/cores it generally does a good job. Why? Because the operating system takes care of it.

                    Of course not. I was responding specifically in the context of counting threads based on the number of processors you have. That's pointless in .NET. Yes, the OS will do some thread parallelization for you, but if you actively want to use those processors you have to use lower level languages with specialized libraries. There's nothing wrong with threading, but you have to understand your problem and the limitations of various solutions before you start creating tons of threads thinking it will solve your problem, as Luc accurately points out.


                    The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #16

                    Patrick Sears wrote:

                    but you have to understand your problem and the limitations of various solutions before you start

                    Sorry but that is counter the VB way of life. You "start" by dragging and dropping controls onto a form. All the rest of the development is insignificant overpaid typing.

                    P 1 Reply Last reply
                    0
                    • L led mike

                      Patrick Sears wrote:

                      but you have to understand your problem and the limitations of various solutions before you start

                      Sorry but that is counter the VB way of life. You "start" by dragging and dropping controls onto a form. All the rest of the development is insignificant overpaid typing.

                      P Offline
                      P Offline
                      Patrick Etc
                      wrote on last edited by
                      #17

                      led mike wrote:

                      Sorry but that is counter the VB way of life. You "start" by dragging and dropping controls onto a form. All the rest of the development is insignificant overpaid typing.

                      Hehehe, touche' :-D


                      The early bird who catches the worm works for someone who comes in late and owns the worm farm. -- Travis McGee

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups