From 626d49cc9eedf58cdc5b0c5eb90ac01fec808d33 Mon Sep 17 00:00:00 2001 From: Bredor <40574176+freedan42x@users.noreply.github.com> Date: Mon, 11 Dec 2023 20:59:22 +0200 Subject: [PATCH] Create 6 kyu - Wait without blocking.cpp --- cpp/6 kyu - Wait without blocking.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 cpp/6 kyu - Wait without blocking.cpp diff --git a/cpp/6 kyu - Wait without blocking.cpp b/cpp/6 kyu - Wait without blocking.cpp new file mode 100644 index 0000000..8575c3e --- /dev/null +++ b/cpp/6 kyu - Wait without blocking.cpp @@ -0,0 +1,21 @@ +#include +#include + +class Timer +{ + const size_t seconds_; + const std::function callback_; + +public: + explicit Timer(const size_t seconds, const std::function& callback) + : seconds_(seconds), callback_(callback) { }; + + void Start() const + { + std::thread t{[this]() { + std::this_thread::sleep_for(std::chrono::seconds(seconds_)); + callback_(); + }}; + t.detach(); + } +};