-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: k4yt3x <[email protected]>
- Loading branch information
Showing
4 changed files
with
122 additions
and
17 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
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,34 @@ | ||
#ifndef TIMER_H | ||
#define TIMER_H | ||
|
||
#include <atomic> | ||
#include <chrono> | ||
#include <cstdint> | ||
#include <thread> | ||
|
||
class Timer { | ||
public: | ||
Timer(); | ||
~Timer(); | ||
|
||
void start(); | ||
void pause(); | ||
void resume(); | ||
void stop(); | ||
|
||
bool is_running() const; | ||
bool is_paused() const; | ||
int64_t get_elapsed_time() const; | ||
|
||
private: | ||
std::atomic<bool> running; | ||
std::atomic<bool> paused; | ||
std::thread timer_thread; | ||
int64_t elapsed_time; | ||
std::chrono::steady_clock::time_point start_time; | ||
std::chrono::steady_clock::time_point pause_start_time; | ||
|
||
void update_elapsed_time(); | ||
}; | ||
|
||
#endif // TIMER_H |
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,76 @@ | ||
#include "timer.h" | ||
|
||
#include <sys/types.h> | ||
|
||
Timer::Timer() : running(false), paused(false), elapsed_time(0) {} | ||
|
||
Timer::~Timer() { | ||
stop(); | ||
} | ||
|
||
void Timer::start() { | ||
if (running) { | ||
return; | ||
} | ||
|
||
running = true; | ||
paused = false; | ||
elapsed_time = 0; | ||
start_time = std::chrono::steady_clock::now(); | ||
|
||
timer_thread = std::thread([this]() { | ||
while (running) { | ||
if (!paused) { | ||
update_elapsed_time(); | ||
} | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); | ||
} | ||
}); | ||
} | ||
|
||
void Timer::pause() { | ||
if (running && !paused) { | ||
paused = true; | ||
pause_start_time = std::chrono::steady_clock::now(); | ||
} | ||
} | ||
|
||
void Timer::resume() { | ||
if (running && paused) { | ||
paused = false; | ||
auto pause_end_time = std::chrono::steady_clock::now(); | ||
auto pause_duration = | ||
std::chrono::duration_cast<std::chrono::milliseconds>(pause_end_time - pause_start_time) | ||
.count(); | ||
start_time += std::chrono::milliseconds(pause_duration); | ||
} | ||
} | ||
|
||
void Timer::stop() { | ||
running = false; | ||
if (timer_thread.joinable()) { | ||
timer_thread.join(); | ||
} | ||
update_elapsed_time(); | ||
} | ||
|
||
bool Timer::is_running() const { | ||
return running; | ||
} | ||
|
||
bool Timer::is_paused() const { | ||
return paused; | ||
} | ||
|
||
int64_t Timer::get_elapsed_time() const { | ||
return elapsed_time; | ||
} | ||
|
||
void Timer::update_elapsed_time() { | ||
if (running && !paused) { | ||
auto current_time = std::chrono::steady_clock::now(); | ||
elapsed_time = | ||
std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time) | ||
.count(); | ||
} | ||
} |
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