• 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_low_level.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 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/hpx_init.hpp>
12
#include <hpx/include/runtime.hpp>
13
#include <hpx/include/util.hpp>
14
#include <hpx/iostream.hpp>
15
#include <hpx/modules/io_service.hpp>
16

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

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

28
    p->set_value(0);    // notify the waiting HPX thread and return a value
×
29
}
×
30

31
// This function will be executed by an HPX thread
32
hpx::future<int> async_io(char const* string_to_write)
×
33
{
34
    std::shared_ptr<hpx::promise<int>> p =
35
        std::make_shared<hpx::promise<int>>();
36

37
    // Get a reference to one of the IO specific HPX io_service objects ...
38
    hpx::util::io_service_pool* pool =
39
        hpx::get_runtime().get_thread_pool("io_pool");
×
40

41
    // ... and schedule the handler to run on one of its OS-threads.
42
#if ASIO_VERSION >= 103400
×
43
    ::asio::post(
44
        pool->get_io_service(), hpx::bind(&do_async_io, string_to_write, p));
×
45
#else
46
    pool->get_io_service().post(hpx::bind(&do_async_io, string_to_write, p));
47
#endif
×
48

49
    return p->get_future();
50
}
51

52
int hpx_main()
×
53
{
54
    {
55
        // Initiate an asynchronous IO operation wait for it to complete without
56
        // blocking any of the HPX thread-manager threads.
×
57
        hpx::future<int> f = async_io("Write this string to std::cout");
58

59
        // This will suspend the current HPX thread until the IO operation is
×
60
        // finished.
×
61
        int result = f.get();
×
62

63
        // Print the returned result.
64
        hpx::cout << "HPX-thread: The asynchronous IO operation returned: "
×
65
                  << result << "\n"
66
                  << std::flush;
67
    }
×
68

69
    return hpx::finalize();    // Initiate shutdown of the runtime system.
×
70
}
71

72
int main(int argc, char* argv[])
73
{
74
    return hpx::init(argc, argv);    // Initialize and run HPX.
75
}
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