All threads use same cpu

Posted on 2012-09-03
Last Modified: 2012-09-06

I have a program doing heavy calculations. To reduce calculation time, I want it to utilize all cpu's on the pc in parallell.

If I manually start the program twice, everything works fine. But I want the program to start as many threads as there are cpu's by itself.

So I call AfxBeginThread twice with the calculation function. Even though this works, it still utilizes only 1 cpu. So something seems to be keeping the threads locked to the same cpu.

Ideas welcome!
Question by:khun
    LVL 30

    Expert Comment

    Hi khun,

    do you use any synchronization functionality (i.e. critical section, mutex, ...) within the threads? Maybe this makes onbe thread wait until the other one finished.

    The only other reasons I can image are:
    - You forced the threads to use only one CPU using SetProcessAffinityMask or SetThreadAffinityMask (I guess you didn't :o)
    - You built your app without multi-threading support (but I guess in this case the AfxBeginThread should fail so I guess this isn't the case too)

    LVL 8

    Author Comment

    The only synch functionality is an event created with CreateEvent(NULL, TRUE, FALSE, NULL).

    This is pointed to in a structure handled over to AfXBeginThread. When the event is set (by the dialog which created the working threads), all working threads should stop.

    The threads check the event status with WaitForSingleObject(eventHandle, 0) quite often (to ensure the operation can be interrupted quickly).

    Could it be this that prevents the threads from operating in parallell? Would it help to create an array of events, one for each thread?
    LVL 30

    Accepted Solution

    Hm - such an event object IMO shouldn't be the cause for the problem.

    What does the thread function do? Could you post the code? Maybe it uses some resources which are internally synchronized or something ...

    Further if possible I would suggest to use CreateThread API function to create the threads as long as you don't really need to use CWinThread/AfxBeginThread. It's not very well documented what AfxBeginThread internally does, with CreateThread you can do all you need to implement multithreading in a proper way.

    LVL 8

    Author Comment

    Ok, I'll take a look at your suggestions.
    LVL 8

    Author Comment

    I had forgotten that the calculation routine in it's turn dynamically loads a dll for some calculations. Debug shows that only the first thread succeeds in loading this dll, I guess that's the problem.

    Since your tip about looking for resources that block, instead of suspecting the sync/thread-mechanism, was what got me thinking, I'll accept your solution and continue with questions about how to dynamically load dll's in combination with multithreading in another question.

    Coming soon to an EE-page near you!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now