QThreadPool backup example

Can someone provide an example of using "reserveThread" and / or "releaseThread" from the QThreadPool class? I read the documentation, but I do not understand when you will use these functions. Internet searches look empty.

I use PySide, so Python would be preferable, but C ++ is good too.

+4
source share
1 answer

These methods are used to interact with a thread pool with threads that you manually manage.

, , . reserveThread releaseThread , . . , QThread.

reserveThread : " , , , , , , ( ).

releaseThread : " , ".

: . - ++.

  • :

    QThreadPool pool;
    assert(pool.maxThreadCount() == 4);
    assert(pool.activeThreadCount() == 0);
    
  • : ​​ . , reserveThread:

    MyWorker worker;
    QThread thread;
    worker.moveToThread(&thread);
    thread.start();
    pool.reserveThread();
    assert(pool.activeThreadCount() == 1);
    

    !

  • runnables, . :

    QAtomicInt act = 0;
    QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref();  });
    QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref();  });
    QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref();  });
    QtConcurrent.run(&pool, [&]{ act.ref(); QThread::sleep(60); act.deref();  });
    QThread::sleep(1);
    assert(pool.activeThreadCount() == 4);
    assert(act.load() == 3);
    

    runnables, : , .

  • , . , releaseThread:

    thread.quit();
    thread.wait();
    pool.releaseThread();
    QThread::sleep(1);
    assert(pool.activeThreadCount() == 4);
    assert(act.load() == 4);
    

    runnable, , runnable.

  • , :

    QThread::sleep(60);
    assert(pool.activeThreadCount() == 0);
    assert(act.load() == 0);
    
+6

Source: https://habr.com/ru/post/1651305/


All Articles