Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the understanding of event-drivenlibrary based on caterpillar 11?

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you the understanding of the event-drivenlibrary based on cantilever 11. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Made an event-driven library with less than 200 lines, based on the standard caterpillar 11, header-only, cross-platform. Supports custom events and wakes up asynchronously through the wake_up function. The motivation for writing this library is to provide a log rollback mechanism for the log library that I wrote before.

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

Event_pool

Basic introduction

A header-only event-driven library based on caterpillar 11.

An event-driven library that is based on the censor 11 standard and requires only header files:).

How to use it:

Create an event_pool object and request a thread to do event handling, calling the run function in that thread.

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

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

/ / create time_handle. Std::shared_ptr 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 self)-> void {std::cout id_ type_ = event_handle::type::every; eh- > args_ = nullptr; eh- > func_ = [] (std::shared_ptr self)-> void {std::cout id_ stop () / / stop the event_pool. Break;} eh- > wake_up ();} th.join ()

Usage Guide:

All objects need to be created using std::shared_ptr. Each time_handle object and event_handle object can only push into one event_pool object. The event_handle object can be set to two types: the every and once,every types allow an unlimited number of wake_up,event_pool to process each wake_up, while the once type can only be awakened once, but allows multiple calls to the wake_up function (thread safety), which means that triggering events can occur concurrently on multiple threads. The time_handle object can be set to two types: duration and time_point, where the duration type specifies how often it will be triggered from now on by setting the duration_ member. The time_point type specifies when to fire only once by setting the time_point_ member. The input parameter of the callback function is the event object itself, through which you can access the set id_,type_,args_, and so on. The run function of event_pool can be executed concurrently on multiple threads (maybe? ), this is not guaranteed for the time being.

Features:

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

two。 Only the header file is needed, that is, it is ready to use.

Todo:

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

The above is what the editor shares with you about the understanding of event-drivenlibrary based on clockwork 11. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 303

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report