Ral*_*zky 10 c++ concurrency qt future
如果我正确理解了QFutureWatcher文档中的以下代码,那么最后一行之间存在竞争条件:
// Instantiate the objects and connect to the finished signal.
MyClass myObject;
QFutureWatcher<int> watcher;
connect(&watcher, SIGNAL(finished()), &myObject, SLOT(handleFinished()));
// Start the computation.
QFuture<int> future = QtConcurrent::run(...);
watcher.setFuture(future);
Run Code Online (Sandbox Code Playgroud)
如果函数...的QtConcurrent::run(...)下一行之前完成被调用,那么watcher.finished()信号将永远不会被触发.我的假设是否正确?我该如何解决这个bug?
Ada*_*wen 13
来自http://doc.qt.io/qt-4.8/qfuturewatcher.html#setFuture
其中一个信号可能会针对未来的当前状态发出.例如,如果未来已经停止,则将发出完成的信号.
换句话说,如果QtConcurrent::run(...)在setFuture调用之前完成,setFuture仍然会在当前状态下发出信号QFuture.因此,您不需要做任何事情以避免竞争条件.
但是,根据代码的其余部分,您可能需要调用QFuture::waitForFinished()以确保您的MyClass,QFuture并且在完成之前QFutureWatcher不要超出范围. QtConcurrent::run(...)