The Qt help for the static method QThread::yieldCurrentThread() says that you can, as I understand it, "yield" the context of the execution of the current thread to another thread:

Yields execution of any current thread. Note that the operating system decides.

What is the point and for what cases may be the use of this method?

    1 answer 1

    Generally speaking, the yield tells the operating system scheduler that at present the flow does not require CPU time and the scheduler can interrupt its execution before the time allotted to the flow expires and transfer control to another flow. The scheduler may ignore this statement and not transfer control to another thread.

    You can use, for example, to increase the performance of any workflows of multi-threaded applications, thus reducing the performance of auxiliary threads. But it should be borne in mind that productivity will increase only when there are more working threads (really working, not idle!) Than the number of processor cores.

    The benefit of this, IMHO, is dubious, and instead of yield , I personally would use thread priorities. In practice, I have not used it once.

    • Would it be appropriate to use yield if there are a lot of threads (more than the processor cores), the threads work with the network and go on waiting when sending requests? For example, sent a stream request, and executed yield while waiting for a response. - alexis031182
    • 3
      This is hardly appropriate. If the request is synchronous, the thread will still be decelerated by the system until the answer arrives. Moreover, yield in this case will be executed after the arrival of the answer. If the request is asynchronous, then you are probably slowing down the stream yourself. In addition, the streams working with the network usually create a small load on the processor, without choosing the power of one core. Let's just say - it will not be worse, and whether it will be better - HZ. - user194374
    • Understood, then this is not what I need. Thank. - alexis031182
    • 3
      @ alexis031182, yield often used in various lock-free algorithms, in the part where there is a spin-lock. Instead of desperately hanging on checking some flag, you can give out (yield) control, so as not to waste CPU time on an empty check. - ixSci
    • 3
      @ alexis031182, the whole point of the yield is to give the flow when you see that you no longer need it, but expect that you will need it soon. At the same time, there is no operation in your stream that will itself give up the stream (some blocking I / O). For example, I am waiting in a loop while some flag is set. I can agree on what I will do 1000 checks, and then I will give the flow in order not to overload the CPU. In general, the rule is simple: if you do not know how to use yield, do not use it. When you need it, you will understand everything :) - ixSci