基于c++11的event-drivenlibrary的理解是怎样的

发布时间:2021-10-15 15:43:49 作者:柒染
来源:亿速云 阅读:99

这期内容当中小编将会给大家带来有关基于c++11的event-drivenlibrary的理解是怎样的,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

做了一个不到200行的事件驱动库,基于c++11标准,header-only,跨平台。支持自定义事件,通过wake_up函数异步唤醒。写这个库的动机是想为之前自己写的日志库提供日志回滚机制。

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

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

使用指南:

  1. 所有对象均需使用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:

定义更便于使用,减少出错概率的接口。  补充测试。

上述就是小编为大家分享的基于c++11的event-drivenlibrary的理解是怎样的了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 什么是重载?(最文艺的理解)
  2. 网络编程的理解是怎样的

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ event

上一篇:如何解析JavaWeb工程web.xml基本配置过程

下一篇:C语言小知识之使用指针原因是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》