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. Locking code on slot connected to a QWebSocket
Forum Updated to NodeBB v4.3 + New Features

Locking code on slot connected to a QWebSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 354 Views 2 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
    anphetamina
    wrote on 11 Aug 2020, 17:19 last edited by
    #1

    Hello,
    I've setup a method that is being invoked when the binaryMessageReceived is emitted. I need to put the incoming message into a stack that will be consumed by two concurrent threads that will pop the messages as they are received. This is a typical producer-consumer scenario, but I'm having trouble understanding what happens if blocking code is put inside the slot. The access to the message queue is done exclusively, so what happens when new messages are received from the socket and new signals are emitted? Will they be lost or is the socket stuck inside the blocking code and not receive at all?
    Maybe I'm misunderstanding something and I should try another approach.

    Thank you.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Aug 2020, 18:14 last edited by
      #2

      Hi,

      It partly will depend on how the signal is connected. If direct connection then the caller will block. If queued, the events will accumulate and be consumed after the slot is done.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        anphetamina
        wrote on 11 Aug 2020, 19:13 last edited by
        #3

        By "caller" do you mean the slot connected to the signal?
        How do you queue events?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Aug 2020, 19:37 last edited by
          #4

          @anphetamina said in Locking code on slot connected to a QWebSocket:

          By "caller" do you mean the slot connected to the signal?

          No, the objects that emits the signal

          @anphetamina said in Locking code on slot connected to a QWebSocket:

          How do you queue events?

          In the case of signals and slots, you don't, Qt does that.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            anphetamina
            wrote on 11 Aug 2020, 19:56 last edited by
            #5

            Let's say I have a SslEchoClient that encapsulate a QWebSocket and in its constructor this connections is made:

            connect(&m_webSocket, &QWebSocket::connected, this, &SslEchoClient::onConnected);
            
            ...
            void SslEchoClient::onConnected()
            {
                connect(&m_webSocket, &QWebSocket::binaryMessageReceived,
                        this, &SslEchoClient::onBinaryMessageReceived);
            }
            ...
            void SslEchoClient::onBinaryMessageReceived(QByteArray message)
            {
                std::lock_guard lock(mutex) // this will block for a couple of ms
                // put message in stack for later consumption
            }
            
            
            • Will SslEchoClient stop receiving messages from m_webSocket during the stop on the lock_guard?
            • What happens meanwhile SslEchoClient is waiting to acquire that mutex and new messages are received?
            • Will a long wait on that line makes the GUI unresponsive?
            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 11 Aug 2020, 20:07 last edited by
              #6

              If your mutex is locked long enough to start producing noticeable issues, then you should move your network related code in its own thread.

              In any case, why do you need to use the producer/consumer paradigm in your case ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                anphetamina
                wrote on 11 Aug 2020, 20:16 last edited by
                #7

                Because I need to synchronize the access made on a document that is filled both with data coming from a QWebSocket and from user inputs locally. So my idea is to fill up a stack with data coming from my socket that is later used by separate threads that will synchronize the access to the document with the user inputs.

                1 Reply Last reply
                0

                4/7

                11 Aug 2020, 19:37

                • Login

                • Login or register to search.
                4 out of 7
                • First post
                  4/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved