.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Multithreading with Multiple Processors in .NET
 

Multithreading with Multiple Processors in .NET

Hi All

I want to understand how Multi-threading works in Multi-Processor enviorment with multiple CPU's.
Therefore I want to create a small application (WinForms VB.NET)where I have a Long Task that needs to bereplicated on thespawned threads and I take the following input from the user :-
1. Number of Processors
2. Number of Threads

Can you please guide me with some sample code or links on how can this be done.

Thanks

JC
Developer
Javed Chaudhari

Hi,
multi-threading is only a logically instruction-level parallelism under single processor environment, while multiple processor provides the ability of physically instruction-level parallelism. beyond all question, multi-threading application will gain lots of performance improvement under multi-processor environment.
currently, we cannot determine that "which thread runs on which processor" in a .net application, mostly, it is the OS who in charge of thread scheduling for you (get more from this book "Operating Systems: Internals and Design Principles").
so, it is no need to take care about how many processor there are, just launch some time-consuming threads in your application, and let the application run in different machines to see their difference. following code snippet may give some help to you:

static void Main(string[] args)
{
try
{
Console.WriteLine("Inputs threads count and press enter, 'end' for exit.");
string count;
while (!(count = Console.ReadLine().Trim()).Equals("end"))
{
int result = 0;
if (int.TryParse(count, out result))
{
DateTime begin = DateTime.Now;
for (int i = 0; i < result; i++)
{
Thread t1 = new Thread(new ThreadStart(M1));
t1.Start();
t1.Join();
}

DateTime end = DateTime.Now;
Console.WriteLine("Total ticks: " + (end - begin).Ticks.ToString());
}
else
{
Console.WriteLine("Invalide number.");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.WriteLine("Main done.");

Console.ReadLine();
}

public static void M1()
{
for (int i = 1; i < 1000000000; i++)
{
Math.Cos(1/i);
}
}


Thanks,
Eric


Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang
hello,

Usually you shouldn't care about number of processors. You have to deal with number of processors in very rare cases. At first get some knowledge of threading in .NET and in windows platform.

Samples with multithreading:

http://www.google.com/search?hl=en&ei=boevSqiFO5ui_AaDqt3ZDA&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=Multithreading+examples+in+.net&spell=1

Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
Vitaliy Liptchinsky
Use Environment.ProcessorCount to know how many cores the machine has. One "long task" needs one thread.

Hans Passant.
nobugz
Hello

Thanks for sharing your inputs.

Actually the requirment is such that I need to create a sample multithreading application and test its performance on different machines (machine with single CPU, machine with multiple CPU, clusters etc, so I have the enviorment).

Now its matter of creating a generic application where in I take the input from the User like Number of Processors and Number of Threads and test the performance and speed related issues.

I hope I was able to make u understand the requirement.

Thanks

JC
Developer
Javed Chaudhari
Hello

Thanks for sharing your inputs.

Actually the requirment is such that I need to create a sample multithreading application and test its performance on different machines (machine with single CPU, machine with multiple CPU, clusters etc, so I have the enviorment).

Now its matter of creating a generic application where in I take the input from the User like Number of Processors and Number of Threads and test the performance and speed related issues.

I hope I was able to make u understand the requirement.

Thanks

JC
Developer
Javed Chaudhari

Hi,
multi-threading is only a logically instruction-level parallelism under single processor environment, while multiple processor provides the ability of physically instruction-level parallelism. beyond all question, multi-threading application will gain lots of performance improvement under multi-processor environment.
currently, we cannot determine that "which thread runs on which processor" in a .net application, mostly, it is the OS who in charge of thread scheduling for you (get more from this book "Operating Systems: Internals and Design Principles").
so, it is no need to take care about how many processor there are, just launch some time-consuming threads in your application, and let the application run in different machines to see their difference. following code snippet may give some help to you:

static void Main(string[] args)
{
try
{
Console.WriteLine("Inputs threads count and press enter, 'end' for exit.");
string count;
while (!(count = Console.ReadLine().Trim()).Equals("end"))
{
int result = 0;
if (int.TryParse(count, out result))
{
DateTime begin = DateTime.Now;
for (int i = 0; i < result; i++)
{
Thread t1 = new Thread(new ThreadStart(M1));
t1.Start();
t1.Join();
}

DateTime end = DateTime.Now;
Console.WriteLine("Total ticks: " + (end - begin).Ticks.ToString());
}
else
{
Console.WriteLine("Invalide number.");
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Console.WriteLine("Main done.");

Console.ReadLine();
}

public static void M1()
{
for (int i = 1; i < 1000000000; i++)
{
Math.Cos(1/i);
}
}


Thanks,
Eric


Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang

You can use google to search for other answers

Custom Search

More Threads

• Access to Temp-folder is denied.
• Looking (for help to look) under the /clr hood.
• Is gacutil.exe redistributable
• StringBuilder.Replace
• How ToString method is overridden by System.Object type?
• LINQ to SQL and inheritance problem
• Thread Safety different for different hash tables / dictionaries.
• Access to a smartcard key
• Framework 2.0
• Special folders in configSource