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. SIGNAL(Const QByteArray&) Thread Safety Between 2 Threads (QueuedConnection)
Forum Updated to NodeBB v4.3 + New Features

SIGNAL(Const QByteArray&) Thread Safety Between 2 Threads (QueuedConnection)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 2.2k 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.
  • B Offline
    B Offline
    blackflame7000
    wrote on 29 Jul 2016, 18:02 last edited by blackflame7000
    #1

    Lets say I have 2 Threads: A & B (actually objects moved to different threads)

    Thread A will receive a slot call containing an object to serialize to a QByteArray. After serialization, It appends that QByteArray to an existing Member QByteArray that constitutes an outbound buffer to be sent to a TcpSocket. Thread A then emits a const reference to the outbound buffer.
    emit Send(const QByteArray& m_OutBoundByteArray)

    Thread B has a slot called SendData that is connected to Thread A's Send Signal.
    void SendData(const QByteArray& outboundData)
    {
    TcpSocket->write(outboundData).
    }

    My Question is, what happens if Thread A appends to that m_OutBoundByteArray while Thread B is in the middle of writing it to the TcpSocket? If the member variable m_OutBoundByteArray in thread A is grows in size, does reallocation occur underneath the hood which would cause Thread B to read invalid memory? Or does the const reference QByteArray in the slot call to thread B remain invariant and the memory will be guaranteed to be valid?

    Additionally, Thread A will only Append (Never Remove) elements to the m_OutboundByteArray while Thread B is reading from the const reference in the slot call.

    K 1 Reply Last reply 29 Jul 2016, 21:43
    0
    • B blackflame7000
      29 Jul 2016, 18:02

      Lets say I have 2 Threads: A & B (actually objects moved to different threads)

      Thread A will receive a slot call containing an object to serialize to a QByteArray. After serialization, It appends that QByteArray to an existing Member QByteArray that constitutes an outbound buffer to be sent to a TcpSocket. Thread A then emits a const reference to the outbound buffer.
      emit Send(const QByteArray& m_OutBoundByteArray)

      Thread B has a slot called SendData that is connected to Thread A's Send Signal.
      void SendData(const QByteArray& outboundData)
      {
      TcpSocket->write(outboundData).
      }

      My Question is, what happens if Thread A appends to that m_OutBoundByteArray while Thread B is in the middle of writing it to the TcpSocket? If the member variable m_OutBoundByteArray in thread A is grows in size, does reallocation occur underneath the hood which would cause Thread B to read invalid memory? Or does the const reference QByteArray in the slot call to thread B remain invariant and the memory will be guaranteed to be valid?

      Additionally, Thread A will only Append (Never Remove) elements to the m_OutboundByteArray while Thread B is reading from the const reference in the slot call.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 29 Jul 2016, 21:43 last edited by
      #2

      @blackflame7000
      Hello,

      My Question is, what happens if Thread A appends to that m_OutBoundByteArray while Thread B is in the middle of writing it to the TcpSocket?

      Both worker objects will work on their own data copies. When you have a signal-slot connection across threads the arguments are packed and the slot invocation is differed through a event loop event. This means that when you call emit Send(const QByteArray & m_OutBoundByteArray) your byte array will be copied and and event will be put in the receiver object's thread's event loop. That event, when processed, will be your call to the slot.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      B 1 Reply Last reply 30 Jul 2016, 19:34
      4
      • K kshegunov
        29 Jul 2016, 21:43

        @blackflame7000
        Hello,

        My Question is, what happens if Thread A appends to that m_OutBoundByteArray while Thread B is in the middle of writing it to the TcpSocket?

        Both worker objects will work on their own data copies. When you have a signal-slot connection across threads the arguments are packed and the slot invocation is differed through a event loop event. This means that when you call emit Send(const QByteArray & m_OutBoundByteArray) your byte array will be copied and and event will be put in the receiver object's thread's event loop. That event, when processed, will be your call to the slot.

        Kind regards.

        B Offline
        B Offline
        blackflame7000
        wrote on 30 Jul 2016, 19:34 last edited by
        #3

        @kshegunov
        Thank you for clarifying that scenario! It is very much appreciated.

        1 Reply Last reply
        0

        1/3

        29 Jul 2016, 18:02

        • Login

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