很多东西存在很久但不切实使用并不能体会到它存在的深层的意义,往往这些是前人凝结出的精华而你的知识结构当前还不能体会。QFuture就是其中之一。
Qt 多线程探秘1中使用QThread可以完成一个次线程执行的任务,虽然之需要少量的代码,这也是需要工作量的,要知道Qt其实早就有帮我们实现,这便是QFuture。
QFuture存在的意义应该就是能够便于监管线程(通过QFutureWatcher),这方面比较简单,不赘述,主要介绍如何实现探秘1中QThread完成的任务。于是就不得不引入QtConcurrent了(QFuture内部比较简单,应该一般不会主动new一个QFuture出来,而是通过QtConcurrent的静态方法产生一个QFuture,然后通过QFutureWatcher进行监控)。
- Concurrent Map and Map-Reduce
QtConcurrent::map() applies a function to every item in a container, modifying the items in-place.
QtConcurrent::mapped() is like map(), except that it returns a new container with the modifications.
QtConcurrent::mappedReduced() is like mapped(), except that the modified results are reduced or folded into a single result.- Concurrent Filter and Filter-Reduce
QtConcurrent::filter() removes all items from a container based on the result of a filter function.
QtConcurrent::filtered() is like filter(), except that it returns a new