M
Matthijs de Z
Hi,
I've made a test project to see how the CPU usage will react based on
the example given on: http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem(VS.71).aspx
The code is below.
What I want to achieve is to be able to make sure that for a number of
task send to the threadpool (or any other solution), I will only use x
number of CPU’s. With the code below, it doesn’t matter how I set
ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads, when I run the
program, all my CPU’s are consumed. I’ve tested this on a computer
with 2 and with 8 cores. Also the 8 core computer use all cores.
What do I need to do to get it working properly?
Kind regards,
Matthijs
// This example shows how to create an object containing task
// information, and pass that object to a task queued for
// execution by the thread pool.
using System;
using System.Threading;
// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo
{
// State information for the task. These members
// can be implemented as read-only properties, read/write
// properties with validation, and so on, as required.
public string Boilerplate;
public int Value;
public int CurrentACtiveThreads;
// Public constructor provides an easy way to supply all
// the information needed for the task.
public TaskInfo(string text, int number, int activeThreads)
{
Boilerplate = text;
Value = number;
CurrentACtiveThreads = activeThreads;
}
}
public class Example
{
public static void Main()
{
// Create an object containing the information needed
// for the task.
// Queue the task and data.
ThreadPool.SetMaxThreads(2, 1);
ThreadPool.SetMinThreads(1, 1);
int availableThread,completionPortThreads;
for (int i = 0; i < 100; i++)
{
ThreadPool.GetAvailableThreads(out availableThread, out
completionPortThreads);
TaskInfo ti = new TaskInfo("This report displays the
number",i,availableThread);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),
ti);
Thread.Sleep(50);
}
Console.WriteLine("Main thread does some work, then sleeps.");
// If you comment out the Sleep, the main thread exits before
// the ThreadPool task has a chance to run. ThreadPool uses
// background threads, which do not keep the application
// running. (This is a simple example of a race condition.)
Thread.Sleep(100000);
Console.WriteLine("Main thread exits.");
}
// The thread procedure performs the independent task, in this
case
// formatting and printing a very simple report.
//
static void ThreadProc(Object stateInfo)
{
TaskInfo ti = (TaskInfo)stateInfo;
Console.WriteLine(ti.Boilerplate + " " + ti.Value +" " +
ti.CurrentACtiveThreads);
for (int i = 0; i < 10000000; i++)
{
Math.Sqrt(Math.Sin(i));
Math.Tan(Math.Sqrt(Math.Sin(i)));
}
}
}
I've made a test project to see how the CPU usage will react based on
the example given on: http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem(VS.71).aspx
The code is below.
What I want to achieve is to be able to make sure that for a number of
task send to the threadpool (or any other solution), I will only use x
number of CPU’s. With the code below, it doesn’t matter how I set
ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads, when I run the
program, all my CPU’s are consumed. I’ve tested this on a computer
with 2 and with 8 cores. Also the 8 core computer use all cores.
What do I need to do to get it working properly?
Kind regards,
Matthijs
// This example shows how to create an object containing task
// information, and pass that object to a task queued for
// execution by the thread pool.
using System;
using System.Threading;
// TaskInfo holds state information for a task that will be
// executed by a ThreadPool thread.
public class TaskInfo
{
// State information for the task. These members
// can be implemented as read-only properties, read/write
// properties with validation, and so on, as required.
public string Boilerplate;
public int Value;
public int CurrentACtiveThreads;
// Public constructor provides an easy way to supply all
// the information needed for the task.
public TaskInfo(string text, int number, int activeThreads)
{
Boilerplate = text;
Value = number;
CurrentACtiveThreads = activeThreads;
}
}
public class Example
{
public static void Main()
{
// Create an object containing the information needed
// for the task.
// Queue the task and data.
ThreadPool.SetMaxThreads(2, 1);
ThreadPool.SetMinThreads(1, 1);
int availableThread,completionPortThreads;
for (int i = 0; i < 100; i++)
{
ThreadPool.GetAvailableThreads(out availableThread, out
completionPortThreads);
TaskInfo ti = new TaskInfo("This report displays the
number",i,availableThread);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),
ti);
Thread.Sleep(50);
}
Console.WriteLine("Main thread does some work, then sleeps.");
// If you comment out the Sleep, the main thread exits before
// the ThreadPool task has a chance to run. ThreadPool uses
// background threads, which do not keep the application
// running. (This is a simple example of a race condition.)
Thread.Sleep(100000);
Console.WriteLine("Main thread exits.");
}
// The thread procedure performs the independent task, in this
case
// formatting and printing a very simple report.
//
static void ThreadProc(Object stateInfo)
{
TaskInfo ti = (TaskInfo)stateInfo;
Console.WriteLine(ti.Boilerplate + " " + ti.Value +" " +
ti.CurrentACtiveThreads);
for (int i = 0; i < 10000000; i++)
{
Math.Sqrt(Math.Sin(i));
Math.Tan(Math.Sqrt(Math.Sin(i)));
}
}
}