summaryrefslogtreecommitdiffstats
path: root/examples/qtconcurrent/wordcount/doc/src/qtconcurrent-wordcount.qdoc
blob: adefdfdc594fc41c9dc28d50e493f2ba0b34126d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \example wordcount
    \meta tags {threads, console}
    \title Word Count
    \ingroup qtconcurrentexamples
    \examplecategory {Data Processing & I/O}
    \brief Demonstrates how to use the map-reduce algorithm.

    The Qt Concurrent \e {Word Count} example demonstrates the use of the
    map-reduce algorithm when applied to the problem of counting words in a
    collection of files.

    First, the Application starts a QFileDialog to select a starting
    path, and then prints the output to the console.

    \include examples-run.qdocinc

    \section1 Comparing the operations

    Compare a single-threaded, sequential approach to counting the words in
    the text files to a multithreaded approach with mappedReduced():

    \dots
    \snippet wordcount/main.cpp 1
    \dots
    \snippet wordcount/main.cpp 2
    \dots

    The first argument to the \l {QtConcurrent::}{mappedReduced} function is the
    container to operate on. The second argument is the mapping function
    \c {countWords()}. It is called in parallel by multiple threads. The
    third argument is the reducing function \c {reduce()}. It is called
    once for each result returned by the mapping function, and generates the
    final computation result.

    The function returns a QFuture object of type \c WordCount. Call the
    \l {QFuture::}{result} function immediately on this QFuture to block further
    execution until the result becomes available.

    \note The mapping function must be thread-safe since it is called from
    multiple threads.
*/