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. QTimer understanding
Forum Updated to NodeBB v4.3 + New Features

QTimer understanding

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 689 Views
  • 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.
  • J Offline
    J Offline
    JeniaR
    wrote on last edited by
    #1

    Hi,

    Suppose a thread has a timer, which it started. Now it is in the middle of another slot's execution when the timeout occurs. What happens? Will the thread make "context switch" to itself to another slot? As if it were 2 threads? Ie, it will save the state of its working slot and go to timeout slot?

    Thank you !

    JonBJ 1 Reply Last reply
    0
    • J JeniaR

      Hi,

      Suppose a thread has a timer, which it started. Now it is in the middle of another slot's execution when the timeout occurs. What happens? Will the thread make "context switch" to itself to another slot? As if it were 2 threads? Ie, it will save the state of its working slot and go to timeout slot?

      Thank you !

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @JeniaR
      Nope. Without you having separate threads Qt signal/slot execution is synchronous. The timer signal/slot can only be called when Qt execution is back in the main event loop, after finishing any previous slots. Qt signals are not "interrupts".

      Suppose a thread has a timer

      Only just noticed this. If you are using a separate thread for the timer, with its own event loop, then its signals/slots are executed when that thread runs, independent of whatever might be going on in another thread.

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JeniaR
        wrote on last edited by JeniaR
        #3

        @JonB The code is like this:
        QThread A has a member QTimer *p, which is initialized in the following way:

        t = new QTimer();
        t->moveToThread (this);
        t->setSingleShot (true);
        connect(t, SIGNAL(timeout ()),this, SLOT (onTimeout()), Qt:Queued connection);
        t->start();

        So, it's guaranteed that if A is executing a slot, it will finish it and then move to timeout slot?

        Christian EhrlicherC JonBJ 2 Replies Last reply
        0
        • J JeniaR

          @JonB The code is like this:
          QThread A has a member QTimer *p, which is initialized in the following way:

          t = new QTimer();
          t->moveToThread (this);
          t->setSingleShot (true);
          connect(t, SIGNAL(timeout ()),this, SLOT (onTimeout()), Qt:Queued connection);
          t->start();

          So, it's guaranteed that if A is executing a slot, it will finish it and then move to timeout slot?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JeniaR said in QTimer understanding:

          So, it's guaranteed that if A is executing a slot, it will finish it and then move to timeout slot?

          Yes, how should it work otherwise - the event is passed to the receivers event loop and when this event loop can process data, the corresponding slot is executed.
          Don't know why a QTimer needs a separate thread though - it just makes things more complicated without a gain.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • O Offline
            O Offline
            ollarch
            wrote on last edited by
            #5

            You have to think that an EventLoop i like a queue of tasks to do. Tasks are executed in sequential order.

            1 Reply Last reply
            1
            • J JeniaR

              @JonB The code is like this:
              QThread A has a member QTimer *p, which is initialized in the following way:

              t = new QTimer();
              t->moveToThread (this);
              t->setSingleShot (true);
              connect(t, SIGNAL(timeout ()),this, SLOT (onTimeout()), Qt:Queued connection);
              t->start();

              So, it's guaranteed that if A is executing a slot, it will finish it and then move to timeout slot?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @JeniaR
              Answered as per mine & @Christian-Ehrlicher's. The question is why/do you need a separate thread at all here?

              J 1 Reply Last reply
              0
              • JonBJ JonB

                @JeniaR
                Answered as per mine & @Christian-Ehrlicher's. The question is why/do you need a separate thread at all here?

                J Offline
                J Offline
                JeniaR
                wrote on last edited by JeniaR
                #7

                @JonB and @Christian-Ehrlicher I don't understand, where is a separate thread? Thread A is one of the threads in an application, there're other threads which communicate with it, it needs to be separate thread. So, there's only thread A in my example, which creates new QTimer()
                But what I don't understand is why this moveToThread... I think it does nothing, correct me if I'm wrong?

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  ollarch
                  wrote on last edited by
                  #8

                  They are asking you why do you need a thread to have a timer on it.
                  You can have a timer in main thread instead of creating a new thread. So, why do you think you need a thread? Are you gonna do heavy calculations on it or you only want a timer?

                  J 1 Reply Last reply
                  1
                  • O ollarch

                    They are asking you why do you need a thread to have a timer on it.
                    You can have a timer in main thread instead of creating a new thread. So, why do you think you need a thread? Are you gonna do heavy calculations on it or you only want a timer?

                    J Offline
                    J Offline
                    JeniaR
                    wrote on last edited by
                    #9

                    @ollarch That thread does some working and that's why it's required as a separate thread... It's not in the scope of the question. But I received an answer, so thanks @JonB who clarified.

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • J JeniaR

                      @ollarch That thread does some working and that's why it's required as a separate thread... It's not in the scope of the question. But I received an answer, so thanks @JonB who clarified.

                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @JeniaR said in QTimer understanding:

                      That thread does some working and that's why it's required as a separate thread

                      This still does not answer why a QTimer in this thread emits a signal which is handled in the main thread - this makes no sense.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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