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 put the thread to sleep before the desired signal appears?
Forum Updated to NodeBB v4.3 + New Features

How to put the thread to sleep before the desired signal appears?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.7k 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.
  • A Offline
    A Offline
    AlekseyB
    wrote on last edited by AlekseyB
    #1

    I created an instance of QThread to perform asynchronous actions from the main threads. This object has two signals:

    void finished()
    void started()
    

    started() it is necessary to wait before the start of the main threads, and finished() - before creating the message about the end of processing.

    Of course, I can connect each of these signals to the slots using connect(). But it strongly disfigures the code (it is necessary to put all the main code in one slot, and in the other slot to put the code of the completion of processing). The code turns out to be grouped not by functionality, but by "signal-slot" necessity ...

    Is there any method that "sleep" the current thread until the desired signal appears?

    kshegunovK 1 Reply Last reply
    0
    • A AlekseyB

      I created an instance of QThread to perform asynchronous actions from the main threads. This object has two signals:

      void finished()
      void started()
      

      started() it is necessary to wait before the start of the main threads, and finished() - before creating the message about the end of processing.

      Of course, I can connect each of these signals to the slots using connect(). But it strongly disfigures the code (it is necessary to put all the main code in one slot, and in the other slot to put the code of the completion of processing). The code turns out to be grouped not by functionality, but by "signal-slot" necessity ...

      Is there any method that "sleep" the current thread until the desired signal appears?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @AlekseyB said in How to put the thread to sleep before the desired signal appears?:

      started() it is necessary to wait before the start of the main threads, and finished() - before creating the message about the end of processing.

      why, and why? How did you implement the threading, did you override QThread::run?

      The code turns out to be grouped not by functionality, but by "signal-slot" necessity ...

      Which is how event-driven programming works to begin with.

      Is there any method that "sleep" the current thread until the desired signal appears?

      Depends on what is supposed to sleep until which signal appears ... could you elaborate?

      Read and abide by the Qt Code of Conduct

      A 1 Reply Last reply
      5
      • kshegunovK kshegunov

        @AlekseyB said in How to put the thread to sleep before the desired signal appears?:

        started() it is necessary to wait before the start of the main threads, and finished() - before creating the message about the end of processing.

        why, and why? How did you implement the threading, did you override QThread::run?

        The code turns out to be grouped not by functionality, but by "signal-slot" necessity ...

        Which is how event-driven programming works to begin with.

        Is there any method that "sleep" the current thread until the desired signal appears?

        Depends on what is supposed to sleep until which signal appears ... could you elaborate?

        A Offline
        A Offline
        AlekseyB
        wrote on last edited by AlekseyB
        #3

        @kshegunov said in How to put the thread to sleep before the desired signal appears?:

        elaborate

        I need the thread for auxiliary purposes in order to remove long-term operations from the main threads. Before creating main threads, I need to make sure that exist a thread that getting their's tasks.

        At the end of the work of the main threads, I need to make sure that the auxiliary thread also terminated.

        I do not want ancillary tools to specify the logic for building an application.

        Now in the body of the run(), I have only started the loop for processing signals and messages:

        void run()
                {
                    exec();
                }
        
        kshegunovK 1 Reply Last reply
        0
        • A AlekseyB

          @kshegunov said in How to put the thread to sleep before the desired signal appears?:

          elaborate

          I need the thread for auxiliary purposes in order to remove long-term operations from the main threads. Before creating main threads, I need to make sure that exist a thread that getting their's tasks.

          At the end of the work of the main threads, I need to make sure that the auxiliary thread also terminated.

          I do not want ancillary tools to specify the logic for building an application.

          Now in the body of the run(), I have only started the loop for processing signals and messages:

          void run()
                  {
                      exec();
                  }
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          I need the thread for auxiliary purposes in order to remove long-term operations from the main threads.

          Yeah, yeah, that's what's the purpose of threads to begin with.

          Before creating main threads, I need to make sure that exist a thread that getting their's tasks.

          Again, why? You can start the thread at any time you need, there's no need to be so very specific about the exact time (i.e. before main()).

          At the end of the work of the main threads, I need to make sure that the auxiliary thread also terminated.

          That's why you have QThread::wait. Simple example follows:

          int main(int argc, char **argv)
          {
              QApplication app(argc, argv);
          
              QThread thread;
              thread.start();
          
              QObject::connect(&app, &QCoreApplication::aboutToQuit, &thread, &QThread::quit);
          
              // Do other stuff.
              // ... stuff
          
              int returnValue = QApplication::exec();
              thread.wait();
              return returnValue;
          }
          

          Now in the body of the run(), I have only started the loop for processing signals and messages

          For what reason? This is what the default implementation does, so there's no need to override the run method at all.

          Read and abide by the Qt Code of Conduct

          A 1 Reply Last reply
          4
          • kshegunovK kshegunov

            I need the thread for auxiliary purposes in order to remove long-term operations from the main threads.

            Yeah, yeah, that's what's the purpose of threads to begin with.

            Before creating main threads, I need to make sure that exist a thread that getting their's tasks.

            Again, why? You can start the thread at any time you need, there's no need to be so very specific about the exact time (i.e. before main()).

            At the end of the work of the main threads, I need to make sure that the auxiliary thread also terminated.

            That's why you have QThread::wait. Simple example follows:

            int main(int argc, char **argv)
            {
                QApplication app(argc, argv);
            
                QThread thread;
                thread.start();
            
                QObject::connect(&app, &QCoreApplication::aboutToQuit, &thread, &QThread::quit);
            
                // Do other stuff.
                // ... stuff
            
                int returnValue = QApplication::exec();
                thread.wait();
                return returnValue;
            }
            

            Now in the body of the run(), I have only started the loop for processing signals and messages

            For what reason? This is what the default implementation does, so there's no need to override the run method at all.

            A Offline
            A Offline
            AlekseyB
            wrote on last edited by AlekseyB
            #5

            @kshegunov said in How to put the thread to sleep before the desired signal appears?:

            thread.wait()

            Thank you!
            As I understood, there is a blocking method for checking the end of the thread, but there is no method for checking its successful start. In the second case, I can use:

            while(!thread.isRunning ()) yieldCurrentThread ();
            
            kshegunovK 1 Reply Last reply
            0
            • A AlekseyB

              @kshegunov said in How to put the thread to sleep before the desired signal appears?:

              thread.wait()

              Thank you!
              As I understood, there is a blocking method for checking the end of the thread, but there is no method for checking its successful start. In the second case, I can use:

              while(!thread.isRunning ()) yieldCurrentThread ();
              
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              You can use a semaphore:

              QThread thread;
              
              QObject worker;
              worker.moveToThread(&thread);
              
              QSemaphore semaphore;
              QObject::connect(&thread, QThread::started, &worker, [&semaphore] () -> void  {
                  semaphore.release();
              });
              
              thread.start();
              if (!semaphore.tryAcquire(1, 5000))  {
                  // Semaphore was not released in 5 seconds
              }
              
              // Continue on ...
              

              ... but I continue to puzzle over why would you want to block the main thread to begin with ...

              Read and abide by the Qt Code of Conduct

              A 1 Reply Last reply
              3
              • kshegunovK kshegunov

                You can use a semaphore:

                QThread thread;
                
                QObject worker;
                worker.moveToThread(&thread);
                
                QSemaphore semaphore;
                QObject::connect(&thread, QThread::started, &worker, [&semaphore] () -> void  {
                    semaphore.release();
                });
                
                thread.start();
                if (!semaphore.tryAcquire(1, 5000))  {
                    // Semaphore was not released in 5 seconds
                }
                
                // Continue on ...
                

                ... but I continue to puzzle over why would you want to block the main thread to begin with ...

                A Offline
                A Offline
                AlekseyB
                wrote on last edited by
                #7

                @kshegunov said in How to put the thread to sleep before the desired signal appears?:

                but I continue to puzzle over why would you want to block the main thread to begin with

                "I need the thread for auxiliary purposes in order to remove long-term operations from the main threads. Before creating main threads, I need to make sure that exist a thread that getting their's tasks."

                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