Skip to content

A simple mainloop implementation. Allows for synchronization of functions with main threads and calling of repeated tasks.

License

Notifications You must be signed in to change notification settings

PetrFlajsingr/pf_mainloop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);

About

A simple mainloop implementation. Allows for synchronization of functions with main threads and calling of repeated tasks.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published