基于c++11的event-driven library。
做了一个不到200行的事件驱动库,基于c++11标准,header-only,跨平台。支持自定义事件,通过wake_up函数异步唤醒。写这个库的动机是想为之前自己写的日志库提供日志回滚机制。
github:https://github.com/chloro-pn/…
event_pool
基本介绍
a header-only event-driven library based on c++11.
一个基于c++11标准,仅需要头文件的事件驱动库:)。
使用方法:
- 创建event_pool对象并申请一个线程做事件处理,在该线程中调用run函数。
//run the event_pool. std::shared_ptr<event_pool> ev(new event_pool()); std::thread th([=]()->void { ev->run(); });
- 创建event_handle和time_handle对象并设置id_,type_,回调函数func_,上下文args_(如果是time_handle则还要设置触发时间)等,push进event_pool对象。
//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);
- 在需要触发事件的时候调用wake_up函数(time_handle没有wake_up函数,等待时间到达自动触发)。当需要关闭event_pool时,调用stop函数,然后回收线程,没有来得及处理的事件会被丢弃,即使当event_pool 对象完全销毁后,仍然可以调用wake_up函数,此时会直接返回。
while (true) { char buf[1024]; gets(buf); if (buf[0] == 'q') { ev->stop(); // stop the event_pool. break; } eh->wake_up(); } th.join();
使用指南:
- 所有对象均需使用std::shared_ptr创建。
- 每个time_handle对象和event_handle对象只能push进一个event_pool对象。
- event_handle对象可设置两种类型:every和once,every类型允许不限次数的wake_up,event_pool会处理每次wake_up,而once类型只能被唤醒一次,但允许多次调用wake_up函数(线程安全),这意味着可以在多个线程并发的触发事件。
- time_handle对象可设置两种类型:duration和time_point,其中duration类型通过设置duration_成员来指定从此刻开始,每间隔多少时间就触发一次。time_point类型通过设置time_point_成员来指定在哪个时刻仅触发一次。
- 回调函数的输入参数就是该事件对象本身,你可以通过其访问设置的id_,type_,args_等等。
- event_pool的run函数可以在多个线程并发执行(maybe?),这一点暂且不保证。
特点:
1.轻量级,200行源代码,语言层面的跨平台,基于c++11标准。
2.仅需要头文件,即拿即用。
todo:
- 定义更便于使用,减少出错概率的接口。
- 补充测试。
原文地址:https://segmentfault.com/a/1190000021558744
相关推荐
-
c++ 求int数组的长度 c/c++
2019-7-24
-
编译和链接 c/c++
2019-3-29
-
C++内联函数 c/c++
2019-4-1
-
C/C++中的malloc、calloc和realloc c/c++
2019-3-28
-
分布式rpc框架ice学习之helloworld篇 c/c++
2019-4-1
-
Win7的话,可能有十种简单的方法进行提速呢 c/c++
2019-10-7
-
ubuntu下Nginx详解及点播直播服务器搭建 c/c++
2019-3-29
-
学起来:Flutter将支持桌面应用开发 c/c++
2019-5-4
-
C语言使用hiredis访问redis c/c++
2019-9-15
-
new/delete与命名空间 c/c++
2019-9-17