• 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_external.cpp
1
//  Copyright (c) 2007-2013 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 an external OS-thread in HPX and how to
9
// synchronize the result of this IO task with a waiting HPX thread.
10

11
#include <hpx/hpx_init.hpp>
12
#include <hpx/include/runtime.hpp>
13
#include <hpx/iostream.hpp>
14

15
#include <iostream>
16
#include <memory>
17
#include <thread>
18

19
///////////////////////////////////////////////////////////////////////////////
20
struct registration_wrapper
21
{
22
    registration_wrapper(hpx::runtime* rt, char const* name)
23
      : rt_(rt)
24
    {
25
        // Register this thread with HPX, this should be done once for
26
        // each external OS-thread intended to invoke HPX functionality.
27
        // Calling this function more than once will silently fail (will
28
        // return false).
29
        hpx::register_thread(rt_, name);
×
30
    }
31
    ~registration_wrapper()
32
    {
33
        // Unregister the thread from HPX, this should be done once in the
34
        // end before the external thread exists.
35
        hpx::unregister_thread(rt_);
×
36
    }
37

38
    hpx::runtime* rt_;
39
};
40

41
// this function will be executed by an HPX thread
42
void set_value(std::shared_ptr<hpx::promise<int>> p, int result)
×
43
{
44
    // notify the waiting HPX thread and return a value
45
    p->set_value(result);
46
}
×
47

48
// this function will be executed by a dedicated OS thread
49
void do_async_io(char const* string_to_write,
×
50
    std::shared_ptr<hpx::promise<int>> p, hpx::runtime* rt)
51
{
52
    // register this thread in order to be able to call HPX functionality
53
    registration_wrapper wrap(rt, "external-io");
54

55
    // This IO operation will possibly block the IO thread in the
56
    // kernel.
57
    std::cout << "OS-thread: " << string_to_write << std::endl;
×
58

59
    // Create an HPX thread to guarantee that the promise::set_value
60
    // function can be invoked safely.
61
    hpx::threads::thread_init_data data(
62
        hpx::threads::make_thread_function_nullary(hpx::bind(&set_value, p, 0)),
×
63
        "set_value");
64
    hpx::threads::register_thread(data);
×
65
}
×
66

67
// This function will be executed by an HPX thread
68
int io(char const* string_to_write)
×
69
{
70
    std::shared_ptr<hpx::promise<int>> p =
71
        std::make_shared<hpx::promise<int>>();
72

73
    // Create a new external OS-thread and schedule the handler to
74
    // run on one of its OS-threads.
75
    std::thread external_os_thread(
76
        hpx::bind(&do_async_io, string_to_write, p, hpx::get_runtime_ptr()));
×
77

78
    int result = p->get_future().get();
×
79

80
    // wait for the external thread to exit
81
    external_os_thread.join();
×
82

83
    return result;
×
84
}
85

86
int hpx_main()
×
87
{
88
    {
89
        // Initiate an asynchronous IO operation wait for it to complete without
90
        // blocking any of the HPX thread-manager threads.
91
        //
92
        // This will suspend the current HPX thread until the IO operation is
93
        // finished.
94
        int result = io("Write this string to std::cout");
×
95

96
        // Print the returned result.
97
        hpx::cout << "HPX-thread: The asynchronous IO operation returned: "
×
98
                  << result << "\n"
×
99
                  << std::flush;
×
100
    }
101

102
    return hpx::finalize();    // Initiate shutdown of the runtime system.
×
103
}
104

105
int main(int argc, char* argv[])
×
106
{
107
    return hpx::init(argc, argv);    // Initialize and run HPX.
×
108
}
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