CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2008
    Posts
    45

    threading question

    hi guys I have a function that does almost all the work I need to be done the thing is I need the same work done for 4 different classes (sets of data) luckily I have a core quad processor so I thought to spread the work on the 4 different processors (makes sense). so I used "_beginthreadex " procedure and created 3 threads foo1 foo2 foo3 that in their body calls this key function (same function) with different input parameters, so I'd expect the CPU usage to go to 100% (like 25% for each thread i.e. a core for each thread) but when I run the task manager my executable only takes 25% of the CPU usage denoting that only one core is being used and the 4 threads (main and the foos) are running serially on one core :S, why is this happening?? is it because all threads are using the same code?? i.e. same instruction memory so they can't be parallelised??

    any help is appreciated

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: threading question

    Well...one cannot do more than taking a guess....however, it sounds that your 'key' function is simply not reentrant...and thus one only thread can actually execute it safely at a time....

    As I said, without seeing the code it is pretty much impossible to go further...check the link, it outlines some of the requirements a reentrant function needs to meet...

  3. #3
    Join Date
    Dec 2008
    Posts
    91

    Re: threading question

    look at boost::thread (butt i haven't used that)

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405

    Re: threading question

    Quote Originally Posted by compuKidd View Post
    any help is appreciated
    Why don't you set up a small test where each thread is printing something every second or so.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: threading question

    Quote Originally Posted by compuKidd View Post
    hi guys I have a function that does almost all the work I need to be done the thing is I need the same work done for 4 different classes (sets of data) luckily I have a core quad processor so I thought to spread the work on the 4 different processors (makes sense). so I used "_beginthreadex " procedure and created 3 threads foo1 foo2 foo3 that in their body calls this key function (same function) with different input parameters, so I'd expect the CPU usage to go to 100% (like 25% for each thread i.e. a core for each thread) but when I run the task manager my executable only takes 25% of the CPU usage denoting that only one core is being used and the 4 threads (main and the foos) are running serially on one core :S, why is this happening?? is it because all threads are using the same code?? i.e. same instruction memory so they can't be parallelised??
    Are you doing any kind of synchronization? May be you have done that so extensively that the whole parallelization has been serialized to the result that at a time only one thread is actively doing anything while others are blocked on some resource locked by the running thread.

    You can try to test that using a simple program which launches few threads and then does some computation but has no locks/synchronization involved. But don't make it so trivial that the compiler optimizes your code to actually do nothing.

    Moreover, I have read that the OS might have keep your process on just one core in order to efficiently run it depending upon the amount of work your application is doing. This is usually because of competing programs for the same resource. Or bad locality of memory, lower memory availability (due to your program using it up or other programs running on that system) and more reasons that I might not be aware of. Also, could depend on your machine configurations.

    So, the only thing that I can suggest you is to check the synchronization mechanism your have in your application if it is not serializing all the processing you are doing. Seeing the code might help find something interesting.

    There are probably ways to force the application to use multiple cores, but I am not aware of them and they are not usually suggested to be used unless intentionally found out benefitial and good amount of understanding to lower level OS details.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: threading question

    Quote Originally Posted by poolisfun View Post
    look at boost::thread (butt i haven't used that)
    I don't think switching to boost::thread is a solution to this. Using win32 api's is okay since that is inherently targetted to the OP's platform i.e. Windows. Unless you wanted to stress on some of the boost::thread interfaces that would enforce running of parallel pieces of code on multiple processors/cores.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: threading question

    Quote Originally Posted by compuKidd View Post
    luckily I have a core quad processor so I thought to spread the work on the 4 different processors (makes sense).
    There's a general way of making use of multiple cores which is independent of the actual number of cores. It's called fine-grain multithreading. Have a look at Intel TBB for example,

    http://www.threadingbuildingblocks.org/

    The drawback is that it requires you to approach programming in a quite different way and that can be daunting at first.

  8. #8
    Join Date
    Oct 2008
    Posts
    45

    Re: threading question

    ok I looked up reentrant and well the key function uses _getch() and cerr, can this be the reason? and plus I have the main also running the key function, as for synchronisation I'm using non since each thread has a completely independent sets of data. may I add that the key function is templated but all threads uses the same template arguments.

    thank you all for your help

  9. #9
    Join Date
    Oct 2008
    Posts
    45

    Re: threading question

    I removed the _getch() and the cerr but still same problem please help

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: threading question

    Quote Originally Posted by compuKidd View Post
    I removed the _getch() and the cerr but still same problem please help
    I don't know what to suggest. Did you try to write the sample test program to see if all cores are being used as I suggested earlier? Once you have done that and that test program also doesn't use all cores, can you then atleast show that test program to us? If that test program uses all cores then there must be something different happening in your actual program that prevents the parallelization.

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: threading question

    Quote Originally Posted by compuKidd View Post
    ok I looked up reentrant and well the key function uses _getch() and cerr, can this be the reason? and plus I have the main also running the key function, as for synchronisation I'm using non since each thread has a completely independent sets of data. may I add that the key function is templated but all threads uses the same template arguments.
    Certainly having blocking functions (such as '_getch()') won't help reentrancy...however, I am curios why you need such a function in a thread function in the first place. You usually set up threads to do some work in the background - without user interaction. There certainly exists a user-interface model as well but used in different scenarios.

    Furthermore, I am not sure why the main function also need to call the thread function itself...again, this sounds a bit like a design problem.

    As I indicated in my first reply....can you provide the corresponding code since otherwise nobody is really able to help you further.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured