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(); + } +};