-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync_wait_system_timer.cpp
64 lines (48 loc) · 1.65 KB
/
async_wait_system_timer.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Copyright (c) 2023-present DeepGrace (complex dot invoke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/deepgrace/snp
//
#define BOOST_ASIO_HAS_IO_URING
#define BOOST_ASIO_DISABLE_EPOLL
#include <iostream>
#include <snp.hpp>
#include <unifex/then.hpp>
#include <unifex/upon_error.hpp>
// g++ -std=c++23 -Wall -O3 -Os -s -I include -l uring example/async_wait_system_timer.cpp -o /tmp/async_wait_system_timer
namespace net = boost::asio;
using error_code_t = boost::system::error_code;
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <number>" << std::endl;
return 1;
}
net::io_context ioc;
int number = std::stoi(argv[1]);
bool timed_out = false;
net::system_timer timer(ioc);
snp::async_wait(timer, std::chrono::seconds(number))
| unifex::then([&]
{
timed_out = true;
})
| unifex::upon_error([]<typename Error>(Error error)
{
if constexpr(std::is_same_v<Error, error_code_t>)
std::cout << "async_wait system_timer: " << error.message() << std::endl;
})
| snp::start_detached();
assert(!timed_out);
auto begin = std::chrono::system_clock::now();
ioc.run();
auto end = std::chrono::system_clock::now();
auto dur = std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();
std::cout << "async_wait system_timer waited " << dur << " seconds" << std::endl;
assert(timed_out);
return 0;
}