1

I'm working on a project, there we need to implement multi tasking for some functionality. For this we have used .NET's one of the great API - Task Parallel Library (TPL).

Eg. code for TPL (taken from here)

static void Main()
{
    var options = new ParallelOptions()
    {
        MaxDegreeOfParallelism = 2
    };
    List<int> integerList = Enumerable.Range(0,10).ToList();
    Parallel.ForEach(integerList, options, i =>
    {
        Console.WriteLine(@"value of i = {0}, thread = {1}",
            i, Thread.CurrentThread.ManagedThreadId);
    });

    Console.WriteLine("Press any key to exist");
    Console.ReadLine();
} 

As we know, we can use 'MaxDegreeOfParallelism' option to set maximum threads to be used.

Suppose we have 16 virtual core VM, I want to run application that should target only few selected cores say C1, C2, c8, c9.
Please guide me how it can be done using TPL?

Thanks, Vijay

  • Not doable in / with TPL, but if you do your own thread pool it is basicalyl the same question as https://stackoverflow.com/questions/12427265/specify-a-special-cpu-for-a-thread-in-c-sharp - with an answer there. – TomTom Apr 30 '20 at 12:08

2 Answers2

2

This kind of micro-optimisation at runtime is way outside the scope of C#. And it should definitely not be hardcoded into the programm itself. Generally the load balancing features of .NET and the OS will deal with running threads efficiently.

With this kind of difference, the speed rant might apply: https://ericlippert.com/2012/12/17/performance-rant/

You can however set up such limits in the OS, without even looking at the applications code: https://www.techjunkie.com/restrict-apps-cpu-cores-processor-affinity/ This sounds more like what you need/should be using.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • Bad news: I know quite some applications that do that for a good win. Mostly together with NOT interrupting the threads and keeping them looped to check hardware based registers to be FASTER than programs of people that say "should not be hardcoded". And yes, in some applications that is the difference between "I make money" and "you make money". – TomTom Apr 30 '20 at 12:11
  • 1
    @TomTom Do that association in the OS, **NOT** the code. If 5 Programms ally try to run only on core 4, you run into issues no mater how many cores the machine has. – Christopher Apr 30 '20 at 12:12
  • Ah, but in my particualr case the whole OS exists only to run ONE program that is hardcoded to run on this particualr machine. – TomTom Apr 30 '20 at 12:15
  • @TomTom That is realtime programming. Totally different topic. | Just **picking** something with a GC (like .NET) for realtime programming was a bad idea. Your hardcoding of cores does not make it better. – Christopher Apr 30 '20 at 12:19
  • Well, it works for some ;) The trick with GC is not to produce garbage, you know. – TomTom Apr 30 '20 at 12:20
0

I don't think that you can do that on TPL abstraction level, but probably this answer can be useful for you.