0

I'm in a 4-core machine with Intel processor, Windows 10.

I've made an example app forcing a loop , just to check the task manager and see what happens, something simple like this :

while true do begin end;

I'd expect the cpu will be at 100% with my process running at 100% too ; this was what happened back then when processors have only one core.

But this is not what happens now, my app never uses more then 25% of total CPU power, as shown on task manager. Because it's a 4-core machine, i'm wondering if it's using only one core at full capacity.

Is this 'issue' due the fact i'm running only one thread ?

What should i make to my app really take advantage of all processing power available ?

delphirules
  • 6,443
  • 17
  • 59
  • 108
  • 4
    Yes. If you have *N* virtual processors, you need *N* busy threads to use all your CPU power. For instance, I have a 4-core HT processor, so for me N = 8. You seem to have a 4-core CPU without HT, so your N = 4. – Andreas Rejbrand Oct 22 '21 at 18:07
  • 1
    By the way, why do you write `while True do begin end;` when a simple `while True do;` suffices? – Andreas Rejbrand Oct 22 '21 at 18:08
  • @AndreasRejbrand Thanks ! To be honest, i did not know i don't need to use begin end on this case. – delphirules Oct 22 '21 at 18:12
  • 1
    You can use [`TThread.ProcessorCount`](https://docwiki.embarcadero.com/Libraries/en/System.Classes.TThread.ProcessorCount) to know how many threads you should create. – Remy Lebeau Oct 22 '21 at 18:23
  • 1
    @RemyLebeau ...for an [embarrassingly parallel](https://en.wikipedia.org/wiki/Embarrassingly_parallel) problem, yes, but depending on the algorithm it can be advantageous to use a number of working threads that is different from the number of logical CPU cores available. I don't think there's a one-size-fits-all answer to the number of threads one *should* create for any given problem. Related (albeit with some outdated answers) : [Optimal number of threads per core](https://stackoverflow.com/q/1718465/327083) – J... Oct 22 '21 at 19:04
  • 1
    Related : [Simple Thread Sample Delphi](https://stackoverflow.com/q/3451138/327083) – J... Oct 22 '21 at 19:11
  • 1
    Probably also helpful : [Advice for converting a large monolithic singlethreaded application to a multithreaded architecture?](https://stackoverflow.com/q/2204216/327083) – J... Oct 22 '21 at 19:12
  • 1
    And : [Achieving Thread-Safety](https://stackoverflow.com/q/564319/327083), because it will become a problem quickly once you start down this road. – J... Oct 22 '21 at 19:14
  • 1
    Also, the documentation is a good place to start : [Parallel Programming Library Tutorials](https://docwiki.embarcadero.com/RADStudio/Sydney/en/Parallel_Programming_Library_Tutorials) – J... Oct 22 '21 at 19:18
  • 1
    [Defining thread objects](https://docwiki.embarcadero.com/RADStudio/Sydney/en/Defining_thread_objects_Index) – J... Oct 22 '21 at 19:19
  • 2
    "*I've made an example app forcing a loop , just to check the task manager and see what happens*" - You can do some pretty tricky stuff with Task Manager using careful crafted CPU loads: https://youtu.be/hSoCmAoIMOU – Remy Lebeau Oct 22 '21 at 20:47
  • @RemyLebeau That's... horrifying, lol. – J... Oct 22 '21 at 21:37
  • @RemyLebeau This is probably the nerdiest thing i saw in all my entire life ! lol – delphirules Oct 25 '21 at 00:21

0 Answers0