QtConcurrent::run doesn't move passed arg
-
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 -
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)
-
@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
-
Then please fill a bug report. Maybe there is already one.
3/4