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. Is it safe to call QThread::start() many times?
Forum Updated to NodeBB v4.3 + New Features

Is it safe to call QThread::start() many times?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 469 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on last edited by qwe3
    #1

    Hi,

    Is it safe to quit and start the same QThread many times like this:

    QObject::connect(this, &MainWindow::doSomething, worker, &Worker::doSomething);
    ....
    
    void MainWindow::on_pushButton_clicked() // cancel
    {
        thread->requestInterruption();
        thread->quit();
        thread->wait();
    }
    
    void MainWindow::on_pushButton_2_clicked() // start
    {
        thread->start();
        emit doSomething();
    }
    

    ?

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • Q qwe3

      Hi,

      Is it safe to quit and start the same QThread many times like this:

      QObject::connect(this, &MainWindow::doSomething, worker, &Worker::doSomething);
      ....
      
      void MainWindow::on_pushButton_clicked() // cancel
      {
          thread->requestInterruption();
          thread->quit();
          thread->wait();
      }
      
      void MainWindow::on_pushButton_2_clicked() // start
      {
          thread->start();
          emit doSomething();
      }
      

      ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @qwe3 Should be fine. If the thread is already running second start() call will not do anything.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • Q qwe3

        Hi,

        Is it safe to quit and start the same QThread many times like this:

        QObject::connect(this, &MainWindow::doSomething, worker, &Worker::doSomething);
        ....
        
        void MainWindow::on_pushButton_clicked() // cancel
        {
            thread->requestInterruption();
            thread->quit();
            thread->wait();
        }
        
        void MainWindow::on_pushButton_2_clicked() // start
        {
            thread->start();
            emit doSomething();
        }
        

        ?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #3

        @qwe3 said in Is it safe to call QThread::start() many times?:

        void MainWindow::on_pushButton_2_clicked() // start
        {
        thread->start();
        emit doSomething();
        }

        You could replace this with a connection to
        connect(thread, &QThread::started, worker, &Worker::DoWork)

        Then start the thread when needed.

        @qwe3 said in Is it safe to call QThread::start() many times?:

        thread->requestInterruption();

        Do you handle this properly?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qwe3
          wrote on last edited by
          #4

          @jsulm Thank you!
          @Pl45m4 I don't know it is correct, but I have :

          void Worker::doSomething()
          {
              for(long long i = 0; i < 1000000000; i++)
              {
                  if(QThread::currentThread()->isInterruptionRequested())
                  {
                      return;
                  }
                  if(i%10000000 == 0)
                  {
                      qDebug()<<i;
                  }
              }
              qDebug()<<"end";
          }
          
          Pl45m4P 1 Reply Last reply
          0
          • Q qwe3

            @jsulm Thank you!
            @Pl45m4 I don't know it is correct, but I have :

            void Worker::doSomething()
            {
                for(long long i = 0; i < 1000000000; i++)
                {
                    if(QThread::currentThread()->isInterruptionRequested())
                    {
                        return;
                    }
                    if(i%10000000 == 0)
                    {
                        qDebug()<<i;
                    }
                }
                qDebug()<<"end";
            }
            
            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @qwe3 said in Is it safe to call QThread::start() many times?:

            if(QThread::currentThread()->isInterruptionRequested())
            {
            return;
            }

            Looks good.

            • https://doc.qt.io/qt-5/qthread.html#isInterruptionRequested

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            1

            • Login

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