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. Multiple QDataStreams from different threads - Seg Fault
Forum Updated to NodeBB v4.3 + New Features

Multiple QDataStreams from different threads - Seg Fault

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.5k 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.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    I have a centralized QByteArray with a single QDataStream (A) writing to the array and 2 different QDataStreams (B & C) reading independently from the array. I'm using writeRawData(const char *, int) and readRawData(char *, int) to access the array through the stream. Once I write to the array by using A, I'm signalling B & C that new data is available. B & C then read from the array.

    Everything works perfectly, but once I place C (or B) in a separate QThread, I sometimes (about 10%-20% of the time) get a Seg Fault on readRawData(char *, int).

    So my question: is a QByteArray thread safe if I access it via multiple QDataStreams in different threads?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      The methods of QByteArray and QDataStream are marked as reentrant though. If you follow the link you will arrive on this "page":http://developer.qt.nokia.com/doc/qt-4.8/threads-reentrancy.html#reentrant

      There you find:
      [quote]C++ classes are often reentrant, simply because they only access their own member data. Any thread can call a member function on an instance of a reentrant class, as long as no other thread can call a member function on the same instance of the class at the same time. [/quote]

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        miroslav
        wrote on last edited by
        #3

        Reentrant != thread safe, in this case. I am sure that it is possible the the protocol in the stream gets confused or the internal state of the data stream messed by concurrent reads. You will have to synchronize access.

        I am wondering if QDataStream is really the best device for distributing the contents of the stream to your workers. How about having a single reader and using a semaphore to distribute the chunks to the workers?

        Mirko Boehm | mirko@kde.org | KDE e.V.
        FSFE Fellow
        Qt Certified Specialist

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goocreations
          wrote on last edited by
          #4

          All my readers are sequential, so I thought it would be a good idea (performance-wise) to rather have multiple readers, each with an own pointer to the current position in the thread.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            miroslav
            wrote on last edited by
            #5

            Yes, it is a good idea to use multiple readers. It is not such a good idea to let them read from the data stream concurrently.

            Mirko Boehm | mirko@kde.org | KDE e.V.
            FSFE Fellow
            Qt Certified Specialist

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goocreations
              wrote on last edited by
              #6

              Yes, going to add some blocking mechanisms.

              Thanks miroslav and koahnig!!!

              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