Skip to content

Instantly share code, notes, and snippets.

@odeblic
Created August 11, 2017 09:02
Show Gist options
  • Save odeblic/f741ad9c6dd0e2dedadb6dbe6d05ca16 to your computer and use it in GitHub Desktop.
Save odeblic/f741ad9c6dd0e2dedadb6dbe6d05ca16 to your computer and use it in GitHub Desktop.
Monitoring and heartbeat
#include <thread>
#include <chrono>
class Monitor
{
public:
void onHeartbeat(int id, int status)
{
lastTimestamp = std::chrono::system_clock::now();
}
private:
std::chrono::system_clock::time_point firstTimestamp;
std::chrono::system_clock::time_point lastTimestamp;
};
class Monitored
{
public:
Monitored(int id) : _id(id)
{
_thread = std::thread([this]{
while(true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(500));
sendHeartbeat(_id, _status, _sequenceNumber);
}
});
}
~Monitored()
{
_thread.join();
}
void setStatus(int status)
{
_status = status;
}
private:
int _id;
std::thread _thread;
std::atomic<int> _status;
int _sequenceNumber{0};
};
int main()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment