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. How to pass output parameters through QtConcurrent?

How to pass output parameters through QtConcurrent?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.7k 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.
  • M Offline
    M Offline
    mjepson
    wrote on last edited by
    #1

    I have a function, that fills 2 vectors for me, with training data from cameras. Since it is a stereo setup and this calculation takes a bit of time for each camera, I want to do this parallel.

    So I have now 2 QtConcurrent::run calls to the same function, passing different parameters. After starting these 2 threads, I wait for them to complete.

    Since the function outputs 2 vectors, I pass these vectors as a reference parameter. I can see inside the function that the vectors are properly filled, yet these are empty again when I return to the main function, which was waiting for these threads to finish.

    I can see how this makes sense, as using a reference parameter in another thread is obviously not very wise, since it might be deleted by the thread in which it was created. But now the question remains: what is the best way of using a function with output parameters in QtConcurrent::run?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      You should pass pointers to your vectors instead of references. This is I think because the va_args_list of QtConcurrent::run() make implicit deep copies of the vectors (it calls the QVector<T>(const QVector<T>&) constructor behind the scenes).
      Using pointers you are guaranteed to address the right vector.

      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