File: main.cpp

package info (click to toggle)
qcoro 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: cpp: 8,573; python: 32; xml: 26; makefile: 23; sh: 15
file content (41 lines) | stat: -rw-r--r-- 1,115 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
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "qcorofuture.h"

#include <QCoreApplication>
#include <QTimer>
#include <QtConcurrent>

#include <iostream>
#include <random>

QCoro::Task<> startTask() {
    const auto data = co_await QtConcurrent::run([]() {
        QVector<std::uint64_t> data;
        std::random_device rd{};
        std::mt19937 gen{rd()};
        data.reserve(10'000'000);
        for (int i = 0; i < 10'000'000; ++i) {
            data.push_back(gen());
        }
        return data;
    });

    std::cout << "Generated " << data.size() << " random numbers" << std::endl;

    const auto sum = co_await QtConcurrent::filteredReduced<std::uint64_t>(
        data, [](const auto &) { return true; },
        [](std::uint64_t &interm, std::uint64_t val) { interm += val; },
        QtConcurrent::UnorderedReduce);

    std::cout << "Calculated result: " << sum << std::endl;
    qApp->quit();
}

int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    QTimer::singleShot(0, startTask);
    return app.exec();
}