Dual core optimizations

G

Guest

Hi! I wrote this simple program in C++ (I'm studying some C++ applied to the
windows enviroinment) that creates a child thread:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define MAX_THREADS 1

int j,k;
DWORD WINAPI threadfunc(LPVOID param)
{
for(int i=0;i<100000000;i++)
{
sin(rand()*2*3.1412);
j++;
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
HANDLE t = CreateThread(NULL,0,threadfunc,NULL,0,NULL);
for(int i=0;i<100;i++)
{
cos(rand()*2*3.1412);
k++;
}
WaitForMultipleObjects(MAX_THREADS,&t,TRUE,INFINITE);
printf("Ok, done!%d,%d\n",j,k);

}

The two threads are perfectly synchronized, but... I noticed that the CPU
usage is only 50%! My CPU is an AMD Athlon64 X2 3800+, and I'm using windows
xp pro sp2. Do I have to set some particular optimization in the project
properties panel?
Thank you in advance!
 
T

Tom Widmer [VC++ MVP]

Paolo said:
Hi! I wrote this simple program in C++ (I'm studying some C++ applied to the
windows enviroinment) that creates a child thread:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define MAX_THREADS 1

int j,k;
DWORD WINAPI threadfunc(LPVOID param)
{
for(int i=0;i<100000000;i++)
{
sin(rand()*2*3.1412);
j++;
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
HANDLE t = CreateThread(NULL,0,threadfunc,NULL,0,NULL);
for(int i=0;i<100;i++)

Only 100 iterations!?
{
cos(rand()*2*3.1412);
k++;
}
WaitForMultipleObjects(MAX_THREADS,&t,TRUE,INFINITE);
printf("Ok, done!%d,%d\n",j,k);

}

The two threads are perfectly synchronized, but... I noticed that the CPU
usage is only 50%!

Yes, in your example, the two threads will only run together for a very
short time. CPU usage was probably 100% for a few milliseconds while
main ran its very short loop, and then the main thread hit the Wait
call, and the other thread finished its long loop alone.

My CPU is an AMD Athlon64 X2 3800+, and I'm using windows
xp pro sp2. Do I have to set some particular optimization in the project
properties panel?

No. Windows will automatically schedule threads to all available CPUs
and cores, assuming that the threads can run concurrently (e.g. they are
ready and not blocked).

Tom
 
G

Guest

Hi! I wrote this simple program in C++ (I'm studying some C++ applied to the
windows enviroinment) that creates a child thread:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <math.h>
#define MAX_THREADS 1

int j,k;
DWORD WINAPI threadfunc(LPVOID param)
{
for(int i=0;i<100000000;i++)
{
sin(rand()*2*3.1412);
j++;
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
HANDLE t = CreateThread(NULL,0,threadfunc,NULL,0,NULL);
for(int i=0;i<100;i++)
{
cos(rand()*2*3.1412);
k++;
}
WaitForMultipleObjects(MAX_THREADS,&t,TRUE,INFINITE);
printf("Ok, done!%d,%d\n",j,k);

}

The two threads are perfectly synchronized, but... I noticed that the CPU
usage is only 50%! My CPU is an AMD Athlon64 X2 3800+, and I'm using windows
xp pro sp2. Do I have to set some particular optimization in the project
properties panel?
Thank you in advance!

Hi,

Make sure that your main thread is also doing 100000000 iterations. those
100 iterations you have now will be finished after a couple of ms. this will
cause your program to use only 1 cpu, since the main thread is bloked until
the tread finishes.

Windows will automatically disrbute threads across the available CPUs so you
don't have to do anthing about it.
Just make sure that your program is threadsafe. i.e. if ne thread changes
global information that is used by another thread, appropriate measures
should be taken to prevent race conitions.
 
G

Guest

Whoops! My bad! Thank you both for your answers! I didn't care about that
parameter! The good news is that I don't have anything to set up when i write
software that will be executed on multi-core machines!
Greetings!
Paolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top