Skip to content

Commit

Permalink
Create 6 kyu - Wait without blocking.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
freedan42x authored Dec 11, 2023
1 parent ebe998e commit 626d49c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cpp/6 kyu - Wait without blocking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <thread>
#include <chrono>

class Timer
{
const size_t seconds_;
const std::function<void()> callback_;

public:
explicit Timer(const size_t seconds, const std::function<void()>& callback)
: seconds_(seconds), callback_(callback) { };

void Start() const
{
std::thread t{[this]() {
std::this_thread::sleep_for(std::chrono::seconds(seconds_));
callback_();
}};
t.detach();
}
};

0 comments on commit 626d49c

Please sign in to comment.