Last active
July 12, 2024 20:19
-
-
Save mlfarrell/caec7817ca7a0a660660b572fea11f39 to your computer and use it in GitHub Desktop.
sleeper.cpp
This file contains hidden or 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
#include <chrono> | |
#include <iostream> | |
#include <thread> | |
#include <atomic> | |
#include <cmath> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
using namespace std::chrono; | |
int main() | |
{ | |
atomic_int thingy = 0; | |
atomic<chrono::time_point<chrono::high_resolution_clock>> sent = chrono::high_resolution_clock::now(); | |
thread runner([&thingy, &sent] { | |
int localThingy = 0; | |
std::vector<double> waitsPerFrame; | |
waitsPerFrame.reserve(1000); | |
while(1) | |
{ | |
waitsPerFrame.clear(); | |
while(localThingy > thingy.load()) | |
{ | |
//this_thread::yield(); | |
this_thread::sleep_for(1ms); | |
auto received = chrono::high_resolution_clock::now(); | |
waitsPerFrame.push_back(chrono::duration_cast<chrono::microseconds>(received - sent.load()).count() / 1000.0); | |
} | |
static int t = 0; | |
t++; | |
if(t % 60 == 0) | |
{ | |
cout << "latency range: " << *min_element(waitsPerFrame.begin(), waitsPerFrame.end()) << " ms to "; | |
cout << *max_element(waitsPerFrame.begin(), waitsPerFrame.end()) << " ms" << endl; | |
} | |
localThingy++; | |
//cout << "doing things at 60 fps" << endl; | |
} | |
}); | |
while(1) | |
{ | |
sent = chrono::high_resolution_clock::now(); | |
thingy++; | |
int usec = (int)round((1.0 / 60) * 1000000); | |
std::this_thread::sleep_until(steady_clock::now() + microseconds(usec)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment