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. QtConcurrent::run doesn't move passed arg
QtWS25 Last Chance

QtConcurrent::run doesn't move passed arg

Scheduled Pinned Locked Moved Solved General and Desktop
qtconcurrentmove
4 Posts 2 Posters 441 Views
  • 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.
  • 0 Offline
    0 Offline
    0...-5
    wrote on last edited by
    #1

    I'm trying to run a function in a secondary thread using QtConcurrent::run. I'm passing function arg using move semantics, but it doesn't work. However, if I call my function directly everything is ok. That is my code:

    void doSomething(QByteArrayList data) {
        qDebug() << "passed data is" << data;
    }
    
    int main(int argc, char *argv[]) {
        QByteArrayList data {{"123"}};
        auto watcher = QtConcurrent::run(&doSomething, std::move(data));
        watcher.waitForFinished();
        qDebug() << "data size is" << data.size();
        doSomething(std::move(data));
        qDebug() << "data size is" << data.size();
        return 0;
    }
    

    And that is produced output:

    passed data is ("123")
    data size is 1
    passed data is ("123")
    data size is 0

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Because QtConcurrent::run() takes const refs:

      QFuture<T> run(QThreadPool *pool, const Class *object, T (Class::*fn)(Param1, Param2, Param3, Param4, Param5) const, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5)

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      0 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        Because QtConcurrent::run() takes const refs:

        QFuture<T> run(QThreadPool *pool, const Class *object, T (Class::*fn)(Param1, Param2, Param3, Param4, Param5) const, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5)

        0 Offline
        0 Offline
        0...-5
        wrote on last edited by
        #3

        @Christian-Ehrlicher Ok, I get it. But I think it would be nice to have an rvalue reference overload. The idea is the following: I have some resources to be passed to secondary thread, but I don't need it after that

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then please fill a bug report. Maybe there is already one.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          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