Debian Codegraph @

examples/qtconcurrent/primecounter/doc/src/qtconcurrent-primecounter.qdoc

raw
1 // Copyright (C) 2023 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4 /*!
5 \example primecounter
6 \meta tags {widgets, threads}
7 \title Prime Counter
8 \ingroup qtconcurrentexamples
9 \examplecategory {Data Processing & I/O}
10 \brief Demonstrates how to monitor the progress of concurrent operations.
11
12 The following example demonstrates how to create an interactive and
13 non-blocking QtWidgets application using the QFutureWatcher class and the
14 \l {Concurrent Filter-Reduce} {filteredReduced} functions from
15 \l {Qt Concurrent}. With this example, the user can create a QList of
16 integers that can be resized. The list will be automatically filled with
17 natural numbers starting from 1 up to n. The program will then check for
18 prime numbers within the list and display the total count of prime numbers
19 found.
20
21 \image primecounter.png
22
23 \include examples-run.qdocinc
24
25 \section1 Setting up the connections
26
27 The \l {Qt Concurrent} library provides the
28 \l {Concurrent Filter-Reduce} {filteredReduced} functions, which can operate
29 in two modes:
30 \l {QtConcurrent::ReduceOption} {OrderedReduce and UnorderedReduce}. In
31 \c OrderedReduce mode, the reducing function is called in the order of the
32 original sequence, whereas in \c UnorderedReduce mode, the elements are
33 accessed randomly.
34
35 After configuring the UI with the desired elements, it is necessary to
36 connect them to the signals of the concurrent operations using the Qt
37 \l {Signals & Slots} mechanism. In this example, we use the QFutureWatcher
38 class to monitor the progress of the concurrent operations and provide the
39 signals required to implement the interactive GUI.
40
41 \dots
42 \snippet primecounter/primecounter.cpp 1
43 \dots
44
45 The QFutureWatcher class plays a vital role in this example as it provides
46 the signals required to update the UI in response to changes in the
47 concurrent operations.
48
49 \section1 Starting the concurrent operation
50
51 After connecting all the \l {Signals & Slots}, and when the user presses
52 the QPushButton, the \c {start()} function is called.
53
54 In the \c {start()} function, we call the
55 \l {Concurrent Filter-Reduce} {filteredReduced} function from Qt Concurrent
56 and set the future on the QFutureWatcher member. To ensure that this
57 operation runs truly concurrently, we specify a separate QThreadPool as the
58 first parameter. This approach also avoids any possible blocking in the
59 global thread pool. We pass the QList of integers as the container, a
60 static filter and reduce function, and finally the
61 \l {QtConcurrent::} {ReduceOption} flag.
62
63 \dots
64 \snippet primecounter/primecounter.cpp 2
65 \dots
66
67 Let's examine the filter and reduce functions. These functions are declared
68 static in this example since they do not depend on any member variable.
69 However, they could easily be specified as lambdas or member functions.
70
71 The filter function marks elements for subsequent reduction with the reduce
72 function. This implementation is a simple prime filter. As this function
73 takes a const reference as an argument, it allows thread-safe operation on
74 the container it operates on.
75
76 \dots
77 \snippet primecounter/primecounter.cpp 3
78 \dots
79
80 The reduce function takes a modifiable reference of the same type as the
81 container it operates on as its first parameter. The second parameter is the
82 previously filtered element from the filter function. In this example, we
83 count the number of primes.
84
85 \dots
86 \snippet primecounter/primecounter.cpp 4
87 \dots
88
89 */