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. how to create a new qthread on every call of the function and delete the old one on its completion
Forum Updated to NodeBB v4.3 + New Features

how to create a new qthread on every call of the function and delete the old one on its completion

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 231 Views 2 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.
  • M Offline
    M Offline
    Mandeep.Chaudhary
    wrote on last edited by Mandeep.Chaudhary
    #1

    Hi there is a function 1 on whose call a signal is emitted and a new thread is also created. The connect part in function 1 connects the emitted signal and public slot of another class.

    The thread created in that function 1 should be destroyed after the completion of another class slot.

    And on next call to the function 1 a new thread should be created and so on.

    Currently the thread is not being destroyed. How to fix this i.e. destroy the thread after slot completion.

    Thank You !!!

    void EventListModel::vExtractNextItems()
    {
        emit(fetch());
    
        t = new testthetimer();
    
        workerThread = new QThread();
    
    
        t->moveToThread(workerThread);
    
        connect(this, &EventListModel::fetch, t, &testthetimer::timeout);
    
        qInfo() << "Function 1 call "<< QThread::currentThread();
        workerThread->start();
    
    }
    
    void testthetimer::timeout()
    {
    
        qInfo() << QDateTime::currentDateTime().toString() << " on " << QThread::currentThread();
    
    
    
            qInfo() << "total" << QThread::idealThreadCount();
            this->thread()->quit();
            this->thread()->destroyed();
    
            qInfo() << "running " << this->thread()->isRunning();
            qInfo() << QThread::currentThread();
    }
    

    and the output is

    "Wed Oct 23 17:51:22 2019"  on  QThread(0x7ffe9c803970)
    total 8
    running  true
    current thread is   QThread(0x7ffe9c803970)
    Function 1 call  QThread(0x7ffe9b40efe0)
    qml: DownScale
    "Wed Oct 23 17:51:24 2019"  on  QThread(0x7ffe9b5de260)
    total 8
    running  true
    current thread is   QThread(0x7ffe9b5de260)
    Function 1 call  QThread(0x7ffe9b40efe0)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Call deleteLater in your thread.
      Note that the cleaner way to do that is to emit a signal from your class and connect that to the QThread deleteLater method. This avoids that class to do any thread management that it should not really care about.

      Depending on your use case, you might want to consider the use of QtConcurrent.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mandeep.Chaudhary
        wrote on last edited by
        #3

        yes but with QtConcurrent. i am facing other problems please refer to https://forum.qt.io/topic/108088/how-to-replace-qthread-with-qtconcureent

        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