Re: Utilizing Duo-Core Processors
On May 13, 4:52 pm, "Doug Harrison [MVP]" <d...@mvps.org> wrote:
On Tue, 12 May 2009 20:20:16 -0700 (PDT), hamishd <Hamish.D...@gmail.com>
wrote:
Hello,
I have an application which calls a function (X) to process a
computationally heavy task. When the application is running, task-
manager shows it to be running at 50% of the CPU.
If I run 2 of the applications simultaneously, then each use 50% of
the CPU. As such, I can process 2x as much data in the same time.
You're running a dual core system, right? What you've described in
consistent with SMP with two CPUs.
I have a Intel (R) Core (TM) 2 CPU.
However, I do not want to have to run 2 applications. I want to,
within a single application, call X twice (start 2 threads). Will my
application run at 50% (and as such, each thread 25%) or will each
thread run at 50%?
How do I achieve this?
Threads don't normally run at 50% or 25% or whatever. They run flat out
100% unless they do something to slow themselves down, like entering an
efficient wait state by calling a blocking GetMessage or
WaitForSingleObject. I believe you're observing 50% CPU usage because you
have a single thread running full out on a dual core system. Windows cann=
ot
parallelize your thread to make it run simultaneously in parts on multipl=
e
CPUs or cores, so a single thread will never have more than 50% CPU usage
on a dual core machine; the 50% of two CPUs represents 100% of a single
CPU. You should configure Task Manager to show "One graph per CPU" to get=
a
better picture of things.
Certainly, you can run as many CPU-intensive threads as you have CPUs. Yo=
u
can run more, but you're not going to get better performance, because the
threads will compete for the CPUs. So, for three threads on a dual core,
you could expect something less than 66% full speed for each of the
threads, with the "something less" being due to scheduler overhead. It
could become "significantly less" if the threads have to do a lot of
synchronization with each other or are so memory-intensive they cause
paging. (NB: By "full speed", I mean the speed a single thread would have
on a single CPU at the same clock speed as your CPU(s).)
So starting 2 threads will achieve what i want?
Lets say I have 10 tasks to process.
Currently i'm going:
void ProcessAll()
{
int nTasks = 10;
for(int ii=0;ii<nTasks;ii++){
ProcessTask(ii);
}
}
How to do the 2 processes?
volatile BOOL T1 = FALSE;
volatile BOOL T2 = FALSE;
UINT ProcessTask1(LPVOID pParam)
{
T1 = TRUE;
DoTask(CurrentTask++);
T1 = FALSE;
return 1;
}
UINT ProcessTask2(LPVOID pParam)
{
T2 = TRUE;
DoTask(CurrentTask++);
T2 = FALSE;
return 1;
}
void ProcessAll()
{
int nTasks = 10;
int CurrentTask = 0;
while(CurrentTask < 10){
if(!T1)
AfxBeginThread(ProcessTask1, this);
if(!T2)
AfxBeginThread(ProcessTask2, this);
}
}
I'm sure the above is not how to do it? What is the standard way?