Link to home
Start Free TrialLog in
Avatar of Knut Hunstad
Knut HunstadFlag for Norway

asked on

All threads use same cpu

Hi!

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!
Avatar of Zoppo
Zoppo
Flag of Germany image

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)

ZOPPO
Avatar of Knut Hunstad

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I'll take a look at your suggestions.
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!