C# - How to run multiple tasks on specific multiple cores/processors simultaneously
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: C# - How to run multiple tasks on specific multiple cores/processors simultaneously

  1. #1
    Join Date
    Jun 2013
    Posts
    8

    C# - How to run multiple tasks on specific multiple cores/processors simultaneously

    Hi All,

    I have multiple tasks and i need to run them on specific multiple cores or processors

    simultaneously. For Example, if i have two different tasks or just one task but will run it twice

    i want to run these two tasks on specific cores or processors simultaneously like task1 will run

    on processor1 and task2 will run on processor2 at the same time. i know how to run each specific

    processor but i do not know how to run them simultaneously. I was trying to use multithreading

    then task parallel library to run different tasks on specific processors at the same time but i

    failed. in below code, i was trying to use multithreading but it doesnt work or can i use task

    parallel library???

    PLEASE HELP!!!!!


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;
     
    namespace ConsoleApplication14
    {
        class Program
        {
            
            public static double DumbCountSqr(int dumnum)
            {
     
                Random random = new Random();
                int randomNumber = random.Next(0, 10);
                double result = 0;
                for (int i = 1; i < 10000; i++)
                {
                    for (int j = 1; j < 10000; j++)
                    {
                        result += random.Next(0, 10) * Math.Sqrt(i) * Math.Sqrt(j) + Math.Abs(Math.Sqrt(i));
                    }
                }
                return result;
            }
     
            public static void Main()
            {
     
                Thread t = new Thread(new ThreadStart(Go));
                t.Start();
     
    
                for (int i = 0; i < 1; i++)
                {
     
                    Process Proc = Process.GetCurrentProcess();
                    long AffinityMask = (long)Proc.ProcessorAffinity;
     
                    AffinityMask = 0x0004;//processor    3
                    Proc.ProcessorAffinity = (IntPtr)AffinityMask;
     
                    var result1 = DumbCountSqr(i);
                    Console.WriteLine("result1 =  " + result1);
     
                }
            }
            
            
           public static void Go()
            {
     
                for (int i = 0; i < 1; i++)
                {
                    
                        Process Proc = Process.GetCurrentProcess();
                        long AffinityMask = (long)Proc.ProcessorAffinity;
     
                        AffinityMask = 0x0001;//processor    1
                        Proc.ProcessorAffinity = (IntPtr)AffinityMask;
     
                        var result2 = DumbCountSqr(i);
                        Console.WriteLine("result2 =  " + result2);
       
                }
       
            }
     
        }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,219

    Re: C# - How to run multiple tasks on specific multiple cores/processors simultaneous

    In general, it's best to let the thread scheduler decide which processor/core to use. That being said read up on http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center