Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2007

Tip: Looking for answers? Try searching our database.

Dual core optimizations

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paolo Niccolò Giubelli [Axettone] - 12 Apr 2007 09:26 GMT
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!
Tom Widmer [VC++ MVP] - 12 Apr 2007 13:01 GMT
> Hi! I wrote this simple program in C++ (I'm studying some C++ applied to the
> windows enviroinment) that creates a child thread:
[quoted text clipped - 21 lines]
>     HANDLE t = CreateThread(NULL,0,threadfunc,NULL,0,NULL);
>     for(int i=0;i<100;i++)

Only 100 iterations!?

>     {
>         cos(rand()*2*3.1412);
[quoted text clipped - 7 lines]
> 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
Bruno van Dooren [MVP VC++] - 12 Apr 2007 13:24 GMT
> Hi! I wrote this simple program in C++ (I'm studying some C++ applied to the
> windows enviroinment) that creates a child thread:
[quoted text clipped - 35 lines]
> 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.

Signature

Kind regards,
   Bruno.
   bruno_nos_pam_van_dooren@hotmail.com
   Remove only "_nos_pam"

Paolo Niccolò Giubelli [Axettone] - 12 Apr 2007 14:16 GMT
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
> 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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.