Alex Rivera | Logout

Multi-core usage, threads, thread-pools

Asked 2008-12-28T12:36:52.783
9

I have some questions about multi-threaded programming and multi-core usage.

In particular I'm wondering how the operating system and/or framework (this is .NET) deals with cores that are heavily used.

Here's my questions regarding threads:

  • When a new thread is spawned, what is the algorithm for assigning the thread to a particular core?
    1. Round-robin type of algorithm
    2. Random
    3. The currently least used core
  • If not the currently least used core, would this type of code that determined this dwarf the typical use of a thread and thus just make matters worse?
  • Are threads moved from one core to another during their lifetime? If so, is this to handle cores that for some reason gets "overused" and thus the operating system try to shuffle threads over to less used cores to help the system? If not, again, why not?

My final question, which is basically a reuse of the above, is about the .NET ThreadPool class, which handles things like .BeginInvoke and such. Does this class do any of this stuff? If not, why not, or should it?

Is there any way to tweak this handling, sort of hint at the operating system that this particular thread, please pay a bit more attention to it when you assign it a core, since I know it will use a lot of cpu. Would that make sense? Or would "a lot of cpu" just be relative and thus not really good enough?

Edit
Report

1 Answer

6

In addition to jalf's excellent and comprehensive answer, keep in mind that "Parallel Extensions" (which should be rolled into .NET 4.0) has a lot of code dedicated to allocating work (from queues) to cores evenly, including work stealing, and potentially nuggets like caring which core is "closest" to the memory in which the work resides.

So with .NET 4.0, using things like Parallel.For etc, you should get a lot of this for free. And in general, the OS is clever enough that it just works from the outsider's view. jalf has given a lot of details about what happens under the hood, but most of the time you don't need this level of detail, unless you are ironing out some performance issues with highly threaded code.

answered 2008-12-28T13:25:06.930

Your Answer