分配给正在运行的QFuture [英] Assigning to running QFuture

查看:54
本文介绍了分配给正在运行的QFuture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将另一个QFuture对象分配给已经运行的QFuture对象吗?就像下面的示例一样:

Can I assign another QFuture object to already running QFuture object? Like in the example below:

 QFuture<int> future = QtConcurrent::run(function);
 future = QtConcurrent::run(anotherFunciton); //assume that future is still running at this point

我意识到这个特定的例子没有多大意义,只是为了举例.假设第一个函数的结果不再引起我的兴趣,进行这样的设置是否安全?还是我必须先取消它?如果是这样,我是否必须等待取消完成?

I realize that this particular example doesn't make much sense, it's just for the sake of example. Is it safe to make such an assigment, assuming that the result of the first function doesn't interest me anymore? Or do I have to cancel it first? And if so, do I have to wait for cancelation to complete?

 QFuture<int> future = QtConcurrent::run(function);
 future.cancel();
 future.waitForFinished(); //is waiting necessary?
 future = QtConcurrent::run(anotherFunciton);

推荐答案

来自 http://doc.qt.io/qt-4.8/qtconcurrentrun.html :

请注意,由QtConcurrent :: run()返回的QFuture不支持取消,暂停或进度报告.返回的QFuture可以仅用于查询运行/完成状态和返回函数的值.

Note that the QFuture returned by QtConcurrent::run() does not support canceling, pausing, or progress reporting. The QFuture returned can only be used to query for the running/finished status and the return value of the function.

这意味着即使您愿意,也无法取消QFuture.另外,即使允许取消,为什么还要取消然后等待完成?

Meaning that even if you wanted to, you couldn't cancel your QFuture. Also, even if cancelling was allowed, why would you cancel it and then wait for it to be finished?

来自 http://doc.qt.io/qt-4.8/qthreadpool.html#expiryTimeout-prop :

在一定时间内未使用的线程将过期.这默认的过期超时为30000毫秒(30秒).这可以是使用setExpiryTimeout()进行了更改.设置一个负的到期超时禁用到期机制.

Threads that are unused for a certain amount of time will expire. The default expiry timeout is 30000 milliseconds (30 seconds). This can be changed using setExpiryTimeout(). Setting a negative expiry timeout disables the expiry mechanism.

由于由 QtConcurrent :: run()启动的线程由 QThreadPool 类管理,因此假设您未指定负的到期超时,则应该能够 future = QtConcurrent :: run(anotherFunciton); 没问题;原始线程将到期,并且将退出.但是,这样做的效率令人怀疑.

Since the threads started by QtConcurrent::run() are managed by the QThreadPool class, then assuming you didn't specify a negative expiry timeout, you should be able to future = QtConcurrent::run(anotherFunciton); without issue; the original thread would expire and it would exit. The efficiency of doing this, however, is questionable.

这篇关于分配给正在运行的QFuture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆