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. Thread affinity and signal emission

Thread affinity and signal emission

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 239 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.
  • E Offline
    E Offline
    equeim
    wrote on last edited by
    #1

    Hi all

    I'm confused on the topic of QObject's thread affinity and signal emission. I understand that when you connect to signal using QObject receiver, the slot will be called on receiver's thread. However it's not clear to me on which thread it will be called when receiver is not used (or connection is direct). Documentation says that "The slot is executed in the signalling thread." but which thread is that? Is that QObject::thread() of an object that emits the signal, or could it be some other thread in case object uses background worker thread internally?

    I.e. with following hypothetical code:

    auto* obj = new SomeObject{};
    QObject::connect(obj, &SomeObject::someSignal, [obj] {
        assert(QThread::currentThread() == obj->thread());
    });
    

    Is it guaranteed that condition in lambda will be true for any SomeObject?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      It's the actual thread the code is executed in.

      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
      3
      • Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #3

        As @Christian-Ehrlicher said: The slot is executed in the thread, where the receiver lives.
        Since QThread inherits from QObject, you can simply assign a distinct object name to all your threads and qDebug() << __FUNCTION__ << QThread::currentThread() in your slot (or lambda).

        That behavior can be asserted. A ui object e.g. is likely to crash, if modified from a different thread than the ui thread.

        Software Engineer
        The Qt Company, Oslo

        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