作了一个不到200行的事件驱动库,基于c++11标准,header-only,跨平台。没有使用io复用api,采用promise/future实现。支持自定义事件,经过wake_up函数异步唤醒。写这个库的动机是想为以前本身写的日志库提供日志回滚机制。c++
github:https://github.com/chloro-pn/...git
a header-only event-driven library based on c++11,which uses std::promise/std::future asyn-model.github
一个基于c++11标准,仅须要头文件的事件驱动库:),使用std::promise/std::future异步模型实现。api
//run the event_pool. std::shared_ptr<event_pool> ev(new event_pool()); std::thread th([=]()->void { ev->run(); });
//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);
while (true) { char buf[1024]; gets(buf); if (buf[0] == 'q') { ev->stop(); // stop the event_pool. break; } eh->wake_up(); } th.join();
1.轻量级,200行源代码,语言层面的跨平台,基于c++11标准。promise
2.仅须要头文件,即拿即用。安全