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:
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: