Answer the question
In order to leave comments, you need to log in
How to properly make a thread (QThread) in case of receiving information from the "buffer"?
Greetings!
There is a third-party library, it has some "buffer" (a regular array of 10,000 elements) into which it enters data, but it does not do it uniformly, i.e. it can add 10 elements in 100ms, or it can add 100 elements in 10ms.
I need to read this data accordingly as it appears, and in order not to hang the GUI, I create a new
QThread thread, and in it an infinite loop in which I check if there is new data and, if so, process it.
The problem is that because of this thread, in which there is an infinite loop, the kernel is loaded at 100%, while most of the time the loop is idling, since the data is processed quickly enough, and the new ones have not yet arrived.while(true) { ... }
When you can still live with one such thread, but when there are 10, then all the cores are loaded at 100% and, in fact, do nothing ... And if there are fewer cores in the processor, then the whole thing will start to lag terribly.
How to do it most correctly, so that there would be no such load of cores and that the data would be processed immediately, as soon as it appeared?
Answer the question
In order to leave comments, you need to log in
No interval. Here, either a timer instead of a loop, or stick QThread::msleep(100); before the end of the cycle pass.
As an option - create your own class for receiving data, inside it is a QThread * thd object, in the constructor of your class - moveToThread (thd), in the same class - a timer set to the minimum required time and connected to the slot where the buffer is checked. Although at an interval of 10ms there should be no lags.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question