boostorg / fiber

userland threads
464 stars 108 forks source link

Can I adjust the accuracy of timer? #295

Open zhmt opened 2 years ago

zhmt commented 2 years ago

Failed to achieve FPS 60 ( frame per second) :

auto timenow() {
  return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

int main() {
  boost::fibers::fiber mainFiber([]() 
    {
      while (true) {
        auto now = timenow();
        boost::this_fiber::sleep_for(std::chrono::milliseconds(16));
        std::cout << timenow() - now << "\n";
      }
    });
  mainFiber.join();
}

Outputs ( should be 16 ):

34
27
30
31
31
31
30
31
30
29