Multithreading C++ application

Hi everybody,

Yesterday I finished my C++ application using win32 cosole, only native C++, to be a crossplatform application, using VC++ compiler on windows.

As test this application calculates 10000 times all the basic matrix operations, like determinant, inver, exponential, trace, hermitian, etc...

Now I opened the task manager to visualise the CPU %, I got at maximum about 9% for my application (and 12% all tasks), and my pc has 2 CPUs.

So my questions are:
1) This 9%, says that I can increment the speed calculation without create a multithreading application? There is something that I can do, compiler option etc..?

2) My objective is to get the fastest application that uses all the instantaneus power of my pc, in a crossplatform way. If I well understand I can use multithreading, using for example the <process.h> "default" c library, is there something better? If you know were I can find good tutorials and documentation about, please post it.

Thank you very much for any kind of help.
Looks to me like you're performing too much I/O during the calculations. A program will always use 100% of a CPU if it can. If there are I/O operations, the CPU is forced to wait until the devices finish working. For example, a program that calculates primes and prints each result will run much slower than another that only prints every 1000th result.

If you want portable threading, there's Boost and OpenMP.
With Boost, creating a thread is much like the traditional way. You pass [a pointer to a function] to a constructor and wait until the object says the thread finished.
OpenMP instead does some magic with preprocessor macros. The advantage is that you can parallelize code without changing it too much. another advantage is that it doesn't have dependencies. OpenMP is part of the compiler. Both VC++ and GCC support it, IINM.
Hi Helios,

Thank you very much for your answer. Yes, you are absolutely correct, I got 9% because the application uses many I/O operations, thank you!

Thank you also for Boost and openMP. OpenMP seems to be excellent and very easy to use, I will try to use it with GCC because I'm using VC++ Express.

Only a last point if you know, I'm using in general Qt, as GUI lib, I know that there are 2 classes for threading: QThread (to create a single thread, so you can do multithreading by hand), and QtConcurrent (to split your code in all cpus). Do you know how is the difference beetween Qt thread and OpenMP, or maybe how is the best to mathematical applications?

Another time thank you very much!
Last edited on
Qt's threading support is probably very similar to Boost's, and has the same advantages and disadvantages.
Thank you very much for all answers.
you can increase the cpu time by creating multiple threads. for eg. One for processing and one for I/O. this way you are sure one or more threads will keep on doing processing. or you can configure them in your configuration file also because you have to balance IO threads with processing threads if one set of threads is faster than the other.

you will see your processor rise this way.
Topic archived. No new replies allowed.