From Qt help, it is completely unclear what situations may require functionality of the QThreadPool::reserveThread() and QThreadPool::releaseThread() . It is clear that if we reserve a stream, we do not allow the pool to use it. But just as well, you can simply reduce the number of threads available to the pool by using the setMaxThreadCount() method. Both there and there, the reservation of flows is performed without address, that is, without specifying a specific one. Then in what cases reserve/release can be useful?


In the process of using this functionality, a bug was detected . @ixSci has localized the problem. Corresponding to the description of the correction and reassembly Qt. Well, or wait until they fix it officially.

    1 answer 1

    setMaxThreadCount sets the maximum number of threads in the pool. But in fact they may be less. All tasks that are pooled are distributed between them. reserveThread reserves one thread from this list for user tasks. But if the number of threads in the pool is already equal to the maximum, then reserveThread will add another one on top and the activeThreadCount will be greater than maxThreadCount.

    UPD:

    One of the possible uses for reserveThread / releaseThread is the following. Suppose a task is placed in a trappool that waits for something over the network. If all tasks hang and wait, then the pool will not work very efficiently. By calling reserveThread for the pool, we allow the trap to create another thread if necessary. When the wait is complete and the thread in the pool goes to active work, you need to call releaseThread.

    • That is the opposite? Reserve - an increase in flows in the pool? Interesting. But why then just do not increase the number of threads through setMaxThreadCount() ? It is not very clear what the difference is. Only that the counter activeThreadCount will be more maxThreadCount ? - alexis031182
    • if you increase the number of threads through setMaxThreadCount, then they are all "equal" and the pool will use them. And your important task may not be completed on time. - KoVadim
    • Oh, that's it. The example has become clearer. Thank. - alexis031182