• 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_action.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
// generically schedule an asynchronous IO task onto one of the IO-threads
9
// in HPX (which are OS-threads), how to synchronize the result of this IO
10
// task with a waiting HPX thread, and how to wrap all of this into a HPX
11
// action.
12

13
#include <hpx/config.hpp>
14
#if !defined(HPX_COMPUTE_DEVICE_CODE)
15
#include <hpx/hpx_init.hpp>
16
#include <hpx/include/actions.hpp>
17
#include <hpx/include/parallel_executors.hpp>
18
#include <hpx/include/runtime.hpp>
19
#include <hpx/iostream.hpp>
20

21
#include <iostream>
22
#include <memory>
23
#include <string>
24
#include <vector>
25

26
namespace detail {
27
    // this function will be executed by a dedicated OS thread
28
    void do_async_io(std::string const& string_to_write,
×
29
        std::shared_ptr<hpx::promise<int>> p)
30
    {
31
        // This IO operation will possibly block the IO thread in the
32
        // kernel.
33
        std::cout << "OS-thread: " << string_to_write << std::endl;
34

35
        p->set_value(0);    // notify the waiting HPX thread and return a value
×
36
    }
×
37

38
    // This function will be executed by an HPX thread
39
    hpx::future<int> async_io_worker(std::string const& string_to_write)
×
40
    {
41
        std::shared_ptr<hpx::promise<int>> p =
42
            std::make_shared<hpx::promise<int>>();
43

44
        // Get a reference to one of the IO specific HPX io_service objects ...
45
        hpx::execution::experimental::io_pool_executor executor;
46

47
        // ... and schedule the handler to run on one of its OS-threads.
48
        hpx::post(executor, &do_async_io, string_to_write, p);
×
49

50
        return p->get_future();
×
51
    }
52
}    // namespace detail
53

54
// This function will be called whenever the action async_io_action is
55
// invoked. This allows to remotely execute the async_io.
56
int async_io(std::string const& string_to_write)
×
57
{
58
    hpx::future<int> f = detail::async_io_worker(string_to_write);
×
59
    return f.get();    // simply wait for the IO to finish
×
60
}
61

62
// this defines the type async_io_action
63
HPX_PLAIN_ACTION(async_io, async_io_action)
×
64

65
int hpx_main()
×
66
{
67
    {
68
        // Determine on what locality to run the IO operation. If this
69
        // example is run on one locality, we perform the IO operation
70
        // locally, otherwise we choose one of the remote localities to
71
        // invoke the async_io_action.
72
        hpx::id_type id = hpx::find_here();    // default: local
×
73

74
        std::vector<hpx::id_type> localities = hpx::find_remote_localities();
×
75
        if (!localities.empty())
×
76
            id = localities[0];    // choose the first remote locality
77

78
        // Create an action instance.
79
        async_io_action io;
80

81
        // Initiate an asynchronous (possibly remote) IO operation and
82
        // wait for it to complete without blocking any of the HPX
83
        // thread-manager threads. This will suspend the current HPX
84
        // thread until the IO operation is finished. Other work could
85
        // be executed in the meantime.
86
        int result = io(id, "Write this string to std::cout");
×
87

88
        // Print the returned result.
89
        hpx::cout << "HPX-thread: The asynchronous IO operation returned: "
×
90
                  << result << "\n"
×
91
                  << std::flush;
×
92
    }
×
93

94
    return hpx::finalize();    // Initiate shutdown of the runtime system.
×
95
}
96

97
int main(int argc, char* argv[])
×
98
{
99
    return hpx::init(argc, argv);    // Initialize and run HPX.
×
100
}
101

102
#endif
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

© 2026 Coveralls, Inc