-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 6 kyu - Wait without blocking.cpp
- Loading branch information
1 parent
ebe998e
commit 626d49c
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |