Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How do I suspend a Qt Thread

How do I suspend a Qt Thread

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 4 Posters 711 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.
  • S Offline
    S Offline
    Subbu
    wrote on last edited by Subbu
    #1

    Hi, I have a thread say B. I want to stop this thread B from another thread say A. I tried the following ways. But the thread B keeps running. The program control doesnt come out of the ThreadB event loop. Is there a way to suspend a thread safely and immediately?

    Here is my example code:
    File: main.cpp

    threadA->Start();
    threadB->Start();
    

    threadA: event loop: in fileA.cpp

    void threadA::run()
    {
        while(!m_quit)
        {
            if (threadB->isRunning())
            {
                threadB->quit();
                threadB->wait();            
            }
    }
    

    I also tried it this way to stop the thread B.

    void threadA::run()
    {
        while(!m_quit)
        {
            if (threadB->isRunning())
            {
                threadB->wait();            
                threadB->quit();
            }
    }
    

    Here is the thread B event loop in fileB.cpp

    void threadB::run()
    {
        while(!m_quit)
        {
            //check if USB device is connected
            //Do XYZ
         }
    }
    
    JonBJ 1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      use worker class for the threads and send a signal from worker A to worker B to stop thread B
      https://wiki.qt.io/QThreads_general_usage

      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        @Subbu The default implementation of QThread::run calls exec() that starts and runs an event loop. That loop can be stopped by calling quit(). You have overridden run and there's no Qt event loop running in it, so quit() does nothing. You have your own loop and stop condition using a member variable, so you need to use that i.e.

        void threadA::run()
        {
            while(!m_quit)
            {
                if (threadB->isRunning())
                {
                    threadB->m_quit = true; //or using a setter method if you have one
                }
            }
        }
        

        Is there a way to suspend a thread safely and immediately

        Immediately - no. Threads run independently, possibly on different cores or CPUs. ASAP is the closest thing you can get, but not immediately as in the next CPU instruction.
        Safely - yes, but it takes a bit of effort. Your code is not safe. If thread B is stopped and destroyed just after you call if (threadB->isRunning()) but before threadB->quit(); then your code will crash from calling a method on destroyed object. You need proper synchronization - make sure that both thread objects are alive when they call methods on each other.

        1 Reply Last reply
        2
        • S Subbu

          Hi, I have a thread say B. I want to stop this thread B from another thread say A. I tried the following ways. But the thread B keeps running. The program control doesnt come out of the ThreadB event loop. Is there a way to suspend a thread safely and immediately?

          Here is my example code:
          File: main.cpp

          threadA->Start();
          threadB->Start();
          

          threadA: event loop: in fileA.cpp

          void threadA::run()
          {
              while(!m_quit)
              {
                  if (threadB->isRunning())
                  {
                      threadB->quit();
                      threadB->wait();            
                  }
          }
          

          I also tried it this way to stop the thread B.

          void threadA::run()
          {
              while(!m_quit)
              {
                  if (threadB->isRunning())
                  {
                      threadB->wait();            
                      threadB->quit();
                  }
          }
          

          Here is the thread B event loop in fileB.cpp

          void threadB::run()
          {
              while(!m_quit)
              {
                  //check if USB device is connected
                  //Do XYZ
               }
          }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Subbu
          Everything that @Chris-Kawa said.

          If you are in charge of the code/behaviour of thread B, see void QThread::requestInterruption() and bool QThread::isInterruptionRequested() const for one way of A stopping B. But this may not be your case.

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Subbu
            wrote on last edited by
            #5

            Thanks Fellas for the quick response. You have given me many solutions.

            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