Skip to content

Latest commit

 

History

History
42 lines (37 loc) · 1.2 KB

README.md

File metadata and controls

42 lines (37 loc) · 1.2 KB

pf_mainloop

A simple main loop implementation.

Example usage:

pf::MainLoop::Get()->setOnMainLoop(
    [] (std::chrono::nanosecond frameTime) {
      // main loop code
      if (shouldExit) {
        pf::MainLoop::Get()->stop();
      }
    });
pf::MainLoop::Get()->setOnDone(
    []{
      std::cout << "Main loop ended" << std::endl;
    });

pf::MainLoop::Get()->run(); // blocking operation

The class also supports executing delayed tasks, tasks can be enqueued from a different thread and they'll be run in the main loop's thread:

// if enqueued from main loop's thread the function will be called immediately, 
// otherwise it'll run in the next loop
pf::MainLoop::Get()->enqueue([] {
  std::cout << "Enqueued task" << std::endl;
});
// this function will be called in main loop's thread in ~2 seconds
const auto delay = std::chrono::seconds{2};
pf::MainLoop::Get()->enqueue([] {
  std::cout << "Enqueued delayed task" << std::endl;
}, delay);

Periodic tasks are also supported, these have to be registered from the same thread as the main loop's thread.

const auto period = std::chrono::seconds{5};
pf::MainLoop::Get()->repeat([] {
  std::cout << "Repeated task" << std::endl;
}, period);