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. Safe to pass const references from one thread to another with QtConcurrent::run()?
Forum Updated to NodeBB v4.3 + New Features

Safe to pass const references from one thread to another with QtConcurrent::run()?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.1k 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 Offline
    Q Offline
    qttester5
    wrote on last edited by
    #1

    If I use QtConcurrent::run() to process some number crunching on another thread, is it okay to use a function for run() that accepts const references to objects? Will references be in tact across threads? This would be read-only reference; I am thinking it could avoid copying a lot of data for the number crunching over into a pass-by-value function. The function would not mutate any existing objects, just return a QVector of results based on the data it was passed.

    The docs mention that run() will make a copy of data, so perhaps this would also apply to a reference, in which case I wonder if a pointer would be any better?

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

      As you've already read from the docs, the arguments will be copied. Even if you pass a const reference, Qt will make a copy from your const reference.

      However, what type is your object? If it is an "implicitly shared":http://qt-project.org/doc/qt-5/implicit-sharing.html object (like a QVector), the copying is cheap so you don't have to worry. Otherwise, you will need to pass a pointer, to avoid copying large datasets.

      Also, will your object(s) be modified by any other threads while you do your number-crunching? If so, remember to use a QReadWriteLock for both reading and writing.

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

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qttester5
        wrote on last edited by
        #3

        I will not be modifying any objects in my threaded calculations, just using threads to create new data from heavy calculations.

        Would it better to pass a pointer (for read-only purposes) or to encapsulate what is needed in a QVector, etc, when cheaply sending data to another thread?

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

          [quote author="qttester5" date="1397442407"]Would it better to pass a pointer (for read-only purposes) or to encapsulate what is needed in a QVector, etc, when cheaply sending data to another thread?[/quote]It depends on your existing design. What does your data structure currently look like?

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

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qttester5
            wrote on last edited by
            #5

            It is basically an object with several member variables, some of which are QVectors of other custom types.

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

              [quote author="qttester5" date="1397445217"]It is basically an object with several member variables, some of which are QVectors of other custom types.[/quote]Passing a pointer makes the most sense.

              Packing an object into an implicitly shared container involves copying the object, so you won't save anything there.

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

              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