Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThreadPool after each cycle slows down

QThreadPool after each cycle slows down

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 811 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xcoder
    wrote on last edited by
    #1

    Hello,

    I've been stuck for couple days on QThreadPool slowing down after each cycle.
    Basically, I'm developing simulation. where each QRunnable is responsible for some amount of nodes. Because of that, I don't want to delete the tasks, but just want to update one variable, and rerun all of them. It kinda works, but after each cycle it slows down.

    I managed to reproduce the same results, by simplifying the simulation cycle, it consists of the basic building blocks.

    I initialize QThreadPool

    Construct a QList of my QRunnables "XThread"

    Then I simulate 5000 simulation cycles

    Were in each cycle I 'restart' all the XThreads

    And wait until all of them are done

    However after each cycle, the computation time increases, I don't understand why? I tried clearing the QThreadPool, creating a new one, still the same problem

    Here are my test results, where I print the execution time every 50 simulation cycles.
    On the left simulation cycle, on the right execution time in seconds

    @[13:35:14 - 16/06/2014] 0 0.002
    [13:35:14 - 16/06/2014] 50 0.19
    [13:35:15 - 16/06/2014] 100 0.161
    [13:35:15 - 16/06/2014] 150 0.153
    [13:35:15 - 16/06/2014] 200 0.156
    [13:35:15 - 16/06/2014] 250 0.161
    [13:35:15 - 16/06/2014] 300 0.166
    [13:35:15 - 16/06/2014] 350 0.166
    [13:35:16 - 16/06/2014] 400 0.167
    [13:35:16 - 16/06/2014] 450 0.173
    [13:35:16 - 16/06/2014] 500 0.176
    [13:35:16 - 16/06/2014] 550 0.203
    [13:35:16 - 16/06/2014] 600 0.23
    [13:35:17 - 16/06/2014] 650 0.211
    [13:35:17 - 16/06/2014] 700 0.2
    [13:35:17 - 16/06/2014] 750 0.212
    [13:35:17 - 16/06/2014] 800 0.222
    [13:35:17 - 16/06/2014] 850 0.226
    [13:35:18 - 16/06/2014] 900 0.221
    [13:35:18 - 16/06/2014] 950 0.233
    [13:35:18 - 16/06/2014] 1000 0.261@

    Here is the code, for initializing the XThreads and starting them in simulated simulation cycle:
    @qDebug() << "Testing threads!";
    QThreadPool * pool = new QThreadPool(this);

            QElapsedTimer counter;
            counter.start();
    
            QElapsedTimer timer2;
            timer2.start();
    
            QList<XThread*> threads;
    
            //Lets make 50 "heavy" tasks
                for(int i=0; i<50; i++)
                {
                    XThread* th = new XThread(XThread::TEST);
                    threads.push_back(th);
                }
    
             //In my simulation, I have to reexecute the tasks couple 1000 times, even millions
                for(int k=0; k<5000; k++)
                {
                    foreach(XThread* th, threads)
                    {
                        pool->start(th);
                    }
    
                    if(k mod 50 == 0)
                    {
                        QCoreApplication::processEvents();
                        qDebug()  << k <<  double(timer2.elapsed())/1000.0;
                        timer2.restart();
                    }
                    pool->waitForDone();
                }
    
    
    
            qDebug() << "Time it took to execute (seconds): " << double(counter.elapsed())/1000.0;
            qDebug() << "Thread test finished";@
    

    And here is XThread implementation

    @XThread::XThread(ThreadMode mode) :
    QRunnable()
    {
    num = 0;
    this->mode = mode;
    this->setAutoDelete(false);
    }

    void XThread::run()
    {
    if(mode == TEST)
    {
    for(int i=0; i<100000; i++);
    }
    }@

    I wouldn't be surprised, I'm missing something simple.

    Kind Regards
    Raivis

    Only a biker knows why a dog sticks his head out of a car window.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved