The understanding of ES1en ES2en library based on c++11

  • 2020-07-21 09:29:13
  • OfStack

Made an event driver library of less than 200 lines, based on c++11 standard, ES2en-ES3en, cross-platform. Support for custom events that wake up asynchronously via the wake_up function. The motivation for writing this library is to provide a logging rollback mechanism for the log library you previously wrote yourself.

github: https: / / github com/chloro - pn/event_pool

event_pool

Basic introduction

a header-only event-driven library based on c++11.

An c++11 standard based event driver library that requires only header files :).

Usage:

Create the event_pool object and request a thread for event handling where the run function is called.


  //run the event_pool.
  std::shared_ptr<event_pool> ev(new event_pool());
  std::thread th([=]()->void {
    ev->run();
  });

Create event_handle and time_handle objects and set id_, type_, callback function func_, context args_ (and trigger time if time_handle), push into event_pool object.


  //create time_handle.
  std::shared_ptr<time_handle> h(new time_handle());
  h->id_ = "timer test ";
  h->type_ = time_handle::type::duration;
  h->duration_ = seconds(2);
  h->args_ = nullptr;
  h->func_ = [](std::shared_ptr<time_handle> self)->void {
      std::cout << self->id_ << " wake up !" << std::endl;
  };
  //create event_handle.
  std::shared_ptr<event_handle> eh(new event_handle());
  eh->id_ = "back cout ";
  eh->type_ = event_handle::type::every;
  eh->args_ = nullptr;
  eh->func_ = [](std::shared_ptr<event_handle> self)->void {
    std::cout << self->id_ << " wake up !"<<std::endl;
  };
  //push them into ev.
  ev->push_timer(h);
  ev->push_event(eh);

Call the wake_up function when an event needs to be triggered (time_handle does not have the wake_up function and the wait time reaches the automatic trigger). When event_pool needs to be closed, the stop function is called, and then the thread is reclaimed. Events that have not been processed in time are discarded, and even when the event_pool object is completely destroyed, the wake_up function can still be called, which will be returned directly.


   while (true) {
    char buf[1024];
    gets(buf);
    if (buf[0] == 'q') {
     ev->stop(); // stop the event_pool.
     break;
    }
    eh->wake_up();
   }
   th.join();

User Guide:

All objects are created using std::shared_ptr. Each time_handle object and event_handle object can only carry one event_pool object. The event_handle object can be set to two types: every and once, every allows unlimited number of wake_up,event_pool handles each wake_up, while once can only be woken up once, but allows multiple calls to the wake_up function (thread-safe), which means triggering events that can occur concurrently on multiple threads. The time_handle object can be set to two types: duration and time_point, where the duration type sets the duration_ member to specify how often it fires from this point on. The time_point type specifies at which point the time_point_ member is triggered only once. The input parameter to the callback function is the event object itself, which allows you to access the Settings id_, type_, args_, and so on. event_pool's run function can be executed concurrently on multiple threads (maybe?). This point is not guaranteed for the moment.

Features:

1. Lightweight, 200 lines of source code, language level cross-platform, based on c++11 standard.

2. Only the header file is needed, and it's ready to use.

todo:

Define interfaces that are easier to use and reduce the probability of errors. Supplementary tests.

Related articles: