Load balancing between several Threads
-
Let's imagine that we have two cores (1 physical CPU).
For example, we have some WorkerOne pushed to thread#1. Also we have WorkerTwo into thread#2.
Then we add some heavy loading for WorkerOne. So in result we will have one fully loaded core (50-100%) and one almost spare.
So if I want to balance this heavy loading between these two cores do I need to create an exact number of threads as number of cores?
If it's true then should I store these threads in some threads list and pass a new job to free/random thread?
I'm aware about QThreadPool but in my case I need threads to be persistent. -
@Cocojambo said in Load balancing between several Threads:
I'm aware about QThreadPool but in my case I need threads to be persistent
They are persistant with QThreadPool also, see "QThreadPool manages and recyles individual QThread objects" in the documentation.
"should I store these threads in some threads list and pass a new job to free/random thread?" - yes, you can do this. -
@Cocojambo said in Load balancing between several Threads:
How we can be sure that when I create 4 new threads each of them will be placed in different core?
Does https://doc.qt.io/qt-5/thread-basics.html#using-threads help:
The QtConcurrent module provides an easy interface for distributing work to all of the processor's cores. The threading code is completely hidden in the QtConcurrent framework, so you don't have to take care of the details. However, QtConcurrent can't be used when communication with the running thread is needed, and it shouldn't be used to handle blocking operations.
Does bolded help your situation? Otherwise wait for @jsulm , because both he & you probably know more than I do!
-
@JonB
Well, only partially)https://doc.qt.io/qt-5/qtconcurrent.html
"The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded programs without using low-level threading primitives."
I assume that if we work with low-level QThread we should have not less abilities that high-level API. But QThread doesn't offer any methods to do it. -
@Cocojambo said in Load balancing between several Threads:
I mean that all 4 new threads may be created in one core and we won't get any benefit of such multi threading.
You can't with Qt. But the OS will for sure not put all four threads on one core and leave the other three idle when the four have something to do.