• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/async_io/async_io_simple.cpp
1
//  Copyright (c) 2007-2024 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
// The purpose of this example is to demonstrate how it is possible to
8
// schedule an IO task onto one of the IO-threads in HPX (which are OS-threads)
9
// and how to synchronize the result of this IO task with a waiting HPX thread.
10

11
#include <hpx/assert.hpp>
12
#include <hpx/hpx_init.hpp>
13
#include <hpx/include/parallel_executors.hpp>
14
#include <hpx/include/runtime.hpp>
15
#include <hpx/iostream.hpp>
16

17
#include <functional>
18
#include <iostream>
19

20
// this function will be executed by a dedicated OS thread
21
void do_async_io(char const* string_to_write, int& result)
×
22
{
23
    // This IO operation will possibly block the IO thread in the
24
    // kernel.
25
    std::cout << "OS-thread: " << string_to_write << std::endl;
×
26

27
    result = 1;
×
28
}
×
29

30
// This function will be executed by an HPX thread
31
int async_io(char const* string_to_write)
×
32
{
33
    int result = 0;
×
34

35
    {
36
        // Get a reference to one of the IO specific HPX io_service objects ...
37
        hpx::execution::experimental::io_pool_executor executor;
38

39
        // ... and schedule the handler to run on one of its OS-threads.
40
        hpx::async(executor, &do_async_io, string_to_write, std::ref(result))
×
41
            .get();
×
42
    }
43

44
    HPX_ASSERT(result == 1);
45

46
    return result;    // this will be executed only after result has been set
×
47
}
48

49
int hpx_main()
×
50
{
51
    {
52
        // Execute an asynchronous IO operation wait for it to complete without
53
        // blocking any of the HPX thread-manager threads.
54
        int result = async_io("Write this string to std::cout");
×
55

56
        // Print the returned result.
57
        hpx::cout << "HPX-thread: The asynchronous IO operation returned: "
×
58
                  << result << "\n"
×
59
                  << std::flush;
×
60
    }
61

62
    return hpx::finalize();    // Initiate shutdown of the runtime system.
×
63
}
64

65
int main(int argc, char* argv[])
×
66
{
67
    return hpx::init(argc, argv);    // Initialize and run HPX.
×
68
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc