Skip to content

Commit 15e834a

Browse files
committed
fix
1 parent bdfa5bf commit 15e834a

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

examples/background_task.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,42 @@
55
#include <iostream>
66
#include <thread>
77

8+
namespace {
9+
10+
constexpr auto CounterInterval = std::chrono::seconds{ 1 };
11+
constexpr std::uint64_t CounterLimit = 10;
12+
constexpr auto BackgroundTaskRuntime = std::chrono::milliseconds{ 50 };
13+
constexpr int BackgroundStepsPerRun = 3;
14+
15+
} // namespace
16+
817
int main()
918
{
10-
using namespace std::chrono_literals;
1119
using Clock = std::chrono::steady_clock;
1220

1321
olga_scheduler::EventLoop<Clock> loop;
1422
std::uint64_t counter = 0;
1523
std::uint64_t bg_counter = 0;
1624

17-
auto evt = loop.repeat(1s, [&](const auto& arg) {
25+
auto evt = loop.repeat(CounterInterval, [&](const auto& arg) {
1826
++counter;
1927
std::cout << "counter=" << counter << " now=" << arg.approx_now.time_since_epoch().count() << '\n';
20-
if (counter > 10) {
28+
if (counter > CounterLimit) {
2129
arg.event.cancel();
2230
}
2331
});
2432

2533
auto run_background_step = [&] {
2634
++bg_counter;
2735
std::cout << "bg_counter=" << bg_counter << '\n';
28-
std::this_thread::sleep_for(50ms);
36+
std::this_thread::sleep_for(BackgroundTaskRuntime);
2937
};
3038

3139
while (!loop.isEmpty()) {
3240
const auto spin_result = loop.spin();
3341

3442
if ((counter > 0) && !loop.isEmpty()) {
35-
for (int i = 0; (i < 3) && (Clock::now() < spin_result.next_deadline); ++i) {
43+
for (int i = 0; (i < BackgroundStepsPerRun) && (Clock::now() < spin_result.next_deadline); ++i) {
3644
run_background_step();
3745
}
3846
}

0 commit comments

Comments
 (0)