0
Answer

C# - How to run multiple tasks on specific multiple cores

musa demirtas

musa demirtas

10y
980
1

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!!!!!

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);
}
}
}
}