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. a signal emitted by QMutexLocker locked funtion, will the SLOT connected with this signal also be locked?
Forum Updated to NodeBB v4.3 + New Features

a signal emitted by QMutexLocker locked funtion, will the SLOT connected with this signal also be locked?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 4.4k 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 qtpi

    func1()
    {
    QMutexLocker locker(&mutex);
    emit signal1();
    }
    connect(this,SIGNAL(signal1()),this,SLOT(slot1()));

    slot1();

    so the question is is slot1() also mutexlocked?

    thanks in advance!

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

    @qtpi
    If you don't provide a connection type to QObject::connect, it is; in the sense that it's executed from the signal.
    But(!) calling the slot directly may be dangerous.

    Kind regards.

    Read and abide by the Qt Code of Conduct

    T 1 Reply Last reply
    1
    • kshegunovK kshegunov

      @qtpi
      If you don't provide a connection type to QObject::connect, it is; in the sense that it's executed from the signal.
      But(!) calling the slot directly may be dangerous.

      Kind regards.

      T Offline
      T Offline
      t3685
      wrote on last edited by
      #3

      @kshegunov

      Are you sure? I believe if a slot is called directly or not depended on the thread-affinity of the signal emitting object and the signal receiving object.

      kshegunovK 1 Reply Last reply
      0
      • T t3685

        @kshegunov

        Are you sure? I believe if a slot is called directly or not depended on the thread-affinity of the signal emitting object and the signal receiving object.

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

        @t3685
        By directly I meant invoked as a regular function. E.g.:

        MyClass::MyClass(QObject * parent)
            : QObject(parent);
        {
            QObject::connect(this, &MyClass::mySignal, this, &MyClass::mySlot); //< Same thread affinity, always, thus Qt::DirectConnection
        }
        
        void MyClass::mySlot()
        {
            // Do something dangerous here.
        }
        
        void MyClass::someFunction()
        {
            QMutexLocker locker(&mutex);
            emit mySignal(); //< Safe, 'cause Qt::DirectConnection
        }
        
        void MyClass::someBadFunction()
        {
            mySlot(); //< Ooops! I didn't mean to generate a race condition.
        }
        

        Kind regards.

        Read and abide by the Qt Code of Conduct

        T 1 Reply Last reply
        0
        • kshegunovK kshegunov

          @t3685
          By directly I meant invoked as a regular function. E.g.:

          MyClass::MyClass(QObject * parent)
              : QObject(parent);
          {
              QObject::connect(this, &MyClass::mySignal, this, &MyClass::mySlot); //< Same thread affinity, always, thus Qt::DirectConnection
          }
          
          void MyClass::mySlot()
          {
              // Do something dangerous here.
          }
          
          void MyClass::someFunction()
          {
              QMutexLocker locker(&mutex);
              emit mySignal(); //< Safe, 'cause Qt::DirectConnection
          }
          
          void MyClass::someBadFunction()
          {
              mySlot(); //< Ooops! I didn't mean to generate a race condition.
          }
          

          Kind regards.

          T Offline
          T Offline
          t3685
          wrote on last edited by
          #5

          @kshegunov

          Yes, but only if they have the same thread-affinity. Your statement would be incorrect if the objects are living in different threads.

          kshegunovK Q 2 Replies Last reply
          1
          • T t3685

            @kshegunov

            Yes, but only if they have the same thread-affinity. Your statement would be incorrect if the objects are living in different threads.

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

            @t3685
            This line from the original post:

            connect(this,SIGNAL(signal1()),this,SLOT(slot1()));
            

            guarantees they have the same thread affinity.

            PS.
            It'd be pretty useless to have a mutex at all if they're in different threads, as the connection would default to Qt::QueuedConnection.

            Read and abide by the Qt Code of Conduct

            T 1 Reply Last reply
            0
            • kshegunovK kshegunov

              @t3685
              This line from the original post:

              connect(this,SIGNAL(signal1()),this,SLOT(slot1()));
              

              guarantees they have the same thread affinity.

              PS.
              It'd be pretty useless to have a mutex at all if they're in different threads, as the connection would default to Qt::QueuedConnection.

              T Offline
              T Offline
              t3685
              wrote on last edited by
              #7

              @kshegunov

              Ah missed it :)

              1 Reply Last reply
              0
              • T t3685

                @kshegunov

                Yes, but only if they have the same thread-affinity. Your statement would be incorrect if the objects are living in different threads.

                Q Offline
                Q Offline
                qtpi
                wrote on last edited by
                #8

                @kshegunov

                that is actually good point. In my file I did try to connect this signal with a slot that is running in main thread, the mutexlock will have no effect due to the fact two threads have different affinity.

                I wrote as in post cause I want to make it simpler. But now I fully understand how it works.

                thank both of you.

                kshegunovK 1 Reply Last reply
                0
                • Q qtpi

                  @kshegunov

                  that is actually good point. In my file I did try to connect this signal with a slot that is running in main thread, the mutexlock will have no effect due to the fact two threads have different affinity.

                  I wrote as in post cause I want to make it simpler. But now I fully understand how it works.

                  thank both of you.

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

                  @qtpi
                  You shouldn't need to have mutexes at all when doing signal-slot connections. Qt's designed in such a way that slot invocations are serialized to the thread the receiver lives in when connections are made with Qt::AutoConnection. You need to have a mutex, when you need a mutual exclusion lock. In your case I don't see a reason to have one at all, and to be honest most of the time you won't need it.
                  Perhaps if you expand on what you want to do, we might be able to give some better advice/alternative.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  Q 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    @qtpi
                    You shouldn't need to have mutexes at all when doing signal-slot connections. Qt's designed in such a way that slot invocations are serialized to the thread the receiver lives in when connections are made with Qt::AutoConnection. You need to have a mutex, when you need a mutual exclusion lock. In your case I don't see a reason to have one at all, and to be honest most of the time you won't need it.
                    Perhaps if you expand on what you want to do, we might be able to give some better advice/alternative.

                    Kind regards.

                    Q Offline
                    Q Offline
                    qtpi
                    wrote on last edited by
                    #10

                    @kshegunov

                    nah, I am trying to editing someone's code, and his driver function is a thread that will emit a signal once a new msg is received.

                    I am trying to work on the msg and write some function to react to these incoming msgs. My function is in another class, which will be run in main thread. So I was worried about the synchronization problem if more than one driver are started.

                    kshegunovK 1 Reply Last reply
                    0
                    • Q qtpi

                      @kshegunov

                      nah, I am trying to editing someone's code, and his driver function is a thread that will emit a signal once a new msg is received.

                      I am trying to work on the msg and write some function to react to these incoming msgs. My function is in another class, which will be run in main thread. So I was worried about the synchronization problem if more than one driver are started.

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

                      @qtpi said:

                      So I was worried about the synchronization problem if more than one driver are started.

                      Just make an ordinary connect from his/hers signal to your slot and don't worry about the sync, Qt will do it for you.

                      Read and abide by the Qt Code of Conduct

                      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