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 safety of single instance of QVector, [] operator
Forum Updated to NodeBB v4.3 + New Features

Thread safety of single instance of QVector, [] operator

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

    I'm writing a program in which a QVector<quint64> of several hundred million elements large gets filled with numbers. The vector is pre-allocated (append is never called). The thing is, I want to write to it from several different threads. That is, each thread fills its own assigned section of it, using the [] operator.

    QVector is normally not thread safe, but I was wondering how unsafe it is to have multiple threads writing to it when you know they won't be writing the same elements. Is there meta data that might get messed up?

    In several test runs, it seems to run fine.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      Thing with thread-safety is that "several" tests do not really tell you anything. It's better not to take chances on this.
      Why are you using threads? Is it because the computation of data is really expensive? Or are you worried that adding data to the vector might be a problem.
      If it's the first case you can have the computation in separate threads and have one thread join the data. If it's the second case, you might be off using some other data structure.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        halfgaar
        wrote on last edited by
        #3

        I know that 'several' tests is not a good tests, hence the question.

        It's an expensive operation that I'm optimizing using threads. It takes several gigabytes of RAM, which makes the option using separate data structures and joining them less attractive, because it would require even more memory.

        The QVector is a very efficient way to store my result, because it's preallocated as a contiguous block and doesn't need to store pointers to fragmented segments of data, which would increase the overhead. That's why I went ahead and tested if I could fill the same vector using different threads, knowing they won't be accessing the same elements.

        Using mutexes is not really an option, because of the overhead.

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi,

          The technically correct answer to your question is, "it's undefined". QVector wasn't designed for this use case in mind, so even if it works now there's no guarantee that it will continue to work in the future.

          From an academic point of view, looking at QVector's "source code":http://code.woboq.org/qt5/qtbase/src/corelib/tools/qvector.h.html#_ZN7QVectorixEi, I think it should behaves how you want it to (but remember that you're still relying on undefined behaviour). However, if you ever do anything that causes the QVector to reallocate its internal memory or make a copy of the vector, then all bets are off. Disclaimer: I don't recommend taking this route.

          Why not just use a raw C++ array? This way you don't need to worry about what a QVector might do behind the scenes.
          @
          quint64* myVector = new quint64[N];
          @

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0
          • H Offline
            H Offline
            halfgaar
            wrote on last edited by
            #5

            Actually, using a raw array may very well be option. The class it's in can do its bookkeeping.

            Should have thought of that :)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              t3685
              wrote on last edited by
              #6

              Again though, if the operation itself (as in computing the data) is expensive that you can use threads to do the calculation and use one single thread to store the data.
              If you are worried that storing the data is expensive, using threads won't do you much good because the bottleneck is memory and not computing power.

              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