MCPcopy Create free account
hub / github.com/NVIDIA/stdexec / main

Function main

examples/io_uring.cpp:30–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28#include <iostream>
29
30auto main() -> int
31{
32 exec::io_uring_context context;
33 exec::io_uring_context context2;
34 std::thread io_thread{[&] { context.run_until_stopped(); }};
35 std::thread io_thread2{[&] { context2.run_until_stopped(); }};
36 auto scheduler = context.get_scheduler();
37 auto scheduler2 = context2.get_scheduler();
38 using namespace std::chrono_literals;
39
40 stdexec::sync_wait(exec::when_any(
41 exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; }),
42 exec::schedule_after(scheduler2, 2s) | stdexec::then([] { std::cout << "Hello, 2!\n"; })
43 | stdexec::upon_stopped([] { std::cout << "Hello, 2, stopped.\n"; })));
44
45 stdexec::sync_wait(exec::when_any(
46 exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; })
47 | stdexec::upon_stopped([] { std::cout << "Hello, 1, stopped.\n"; }),
48 exec::schedule_after(scheduler2, 500ms) | stdexec::then([] { std::cout << "Hello, 2!\n"; })
49 | stdexec::upon_stopped([] { std::cout << "Hello, 2, stopped.\n"; })));
50
51 stdexec::sync_wait(stdexec::when_all(
52 stdexec::schedule(scheduler) | stdexec::then([] { std::cout << "Hello, 0!\n"; }),
53 exec::schedule_after(scheduler, 1s) | stdexec::then([] { std::cout << "Hello, 1!\n"; }),
54 exec::schedule_after(scheduler2, 2s) | stdexec::then([] { std::cout << "Hello, 2!\n"; }),
55 exec::schedule_after(scheduler, 3s) | stdexec::then([] { std::cout << "Stop it!\n"; }),
56 exec::finally(exec::schedule_after(scheduler2, 4s),
57 stdexec::just() | stdexec::then([&] { context.request_stop(); })),
58 exec::finally(exec::schedule_after(scheduler, 4s),
59 stdexec::just() | stdexec::then([&] { context2.request_stop(); })),
60 exec::schedule_after(scheduler, 10s) | stdexec::then([] { std::cout << "Hello, world!\n"; })
61 | stdexec::upon_stopped([] { std::cout << "Hello, stopped.\n"; }),
62 exec::schedule_after(scheduler2, 10s) | stdexec::then([] { std::cout << "Hello, world!\n"; })
63 | stdexec::upon_stopped([] { std::cout << "Hello, stopped.\n"; })));
64 io_thread.join();
65 io_thread2.join();
66
67 stdexec::sync_wait(
68 stdexec::schedule(scheduler)
69 | stdexec::then([] { std::cout << "This should not print, because the context is stopped.\n"; })
70 | stdexec::upon_stopped([] { std::cout << "The context is stopped!\n"; }));
71
72 stdexec::sync_wait(
73 stdexec::schedule(scheduler2)
74 | stdexec::then([] { std::cout << "This should not print, because the context is stopped.\n"; })
75 | stdexec::upon_stopped([] { std::cout << "The context is stopped!\n"; }));
76
77 context.reset();
78 io_thread = std::thread{[&] { context.run_until_stopped(); }};
79
80 while (!context.is_running())
81 ;
82 stdexec::sync_wait(exec::when_any(exec::schedule_after(scheduler, 1s)
83 | stdexec::then([] { std::cout << "Hello, 1!\n"; }),
84 exec::schedule_after(scheduler, 500ms)
85 | stdexec::then([] { std::cout << "Hello, 2!\n"; })));
86
87 auto time_point = std::chrono::steady_clock::now() + 1s;

Callers

nothing calls this directly

Calls 8

thenFunction · 0.85
run_until_stoppedMethod · 0.80
is_runningMethod · 0.80
scheduleFunction · 0.50
get_schedulerMethod · 0.45
request_stopMethod · 0.45
joinMethod · 0.45
resetMethod · 0.45

Tested by

no test coverage detected