Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

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!
0
Knut Hunstad
Asked:
Knut Hunstad
  • 3
  • 2
1 Solution
 
ZoppoCommented:
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
0
 
Knut HunstadAuthor Commented:
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?
0
 
ZoppoCommented:
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.

ZOPPO
0
 
Knut HunstadAuthor Commented:
Ok, I'll take a look at your suggestions.
0
 
Knut HunstadAuthor Commented:
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!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now