File: qtconcurrent-wordcount.qdoc

package info (click to toggle)
qt6-base 6.9.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 316,720 kB
  • sloc: cpp: 2,112,334; ansic: 381,848; xml: 142,587; python: 21,632; java: 8,505; asm: 4,009; javascript: 2,290; sh: 1,657; perl: 1,028; makefile: 131
file content (45 lines) | stat: -rw-r--r-- 1,682 bytes parent folder | download | duplicates (3)
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.
*/