Comparison of network libraries libevent, libev, libuv

Comparison of network libraries libevent, libev, libuv

The three network libraries Libevent , libev , and libuv are all asynchronous event libraries implemented in C language ( Asynchronousevent library ).

The asynchronous event library essentially provides asynchronous event notification ( Asynchronous Event Notification , AEN ). The asynchronous event notification mechanism is to call the corresponding callback function for processing according to the occurrence of the event.

Event ( Event ): Event is the core of asynchronous event notification mechanism, such as fd event, timeout event, signal event, timer event. Sometimes it is also called event handler ( EventHandler ), this name is more vivid, because Handler itself represents the data (or the address of the data) and the processing method (callback function) required for processing, more like object-oriented thinking The title.

Event loop ( EventLoop ): wait and distribute events. The event loop is used to manage events.

For applications, these are just APIs provided by the asynchronous event library, which encapsulate the interaction between the asynchronous event library and the operating system. The asynchronous event library will choose a mechanism provided by the operating system to implement a certain event, such as using Unix/Linux The platform's epoll mechanism implements network IO events. When multiple mechanisms are available at the same time, the asynchronous event library will use the optimal mechanism.

 

Compare the next three libraries:

Libevent: the most famous, most widely used and long-standing cross-platform event library;

libev: Compared with libevent , the design is more concise and the performance is better, but the support for Windows is not good enough;

libuv: A cross-platform event library is needed in the process of developing node . They preferred libev , but they also need to support Windows , so they repackaged a set, implemented with libev under Linux , and IOCP under Windows ;


Influence on github:

View ImageView Image

It can be seen that libuv has the greatest influence at present, followed by libevent, and libev pays less attention.


Comparison of priority, event loop, and thread safety dimensions

   characteristic

                       libevent

 libev

 libuv

  

priority

Event organization active in the priority queue, each class thing

Member default preferred first level is the same, it may be by providing

The priority of the event causes it to be processed first

It is also through the priority queue to manage the activation time,

Event priority can also be set

There is no concept of priority, visits in a fixed order

Ask various events


Event loop    

 

    event_base is used to manage events

Event organization active in the priority queue, various events default priority is relative to the same,

You can set the priority of the event to be processed first


Thread safe

Event_base and loop are not thread-safe. An event_base or loop instance can only be accessed in one thread of the user (usually the main thread). Events registered to event_base or loop are all accessed serially, that is, during each execution process. , Will access the activated events in order of priority and execute their callback functions. Therefore, using only a loop situation event_base or lower case, the callback function is executed in parallel relationship does not exist




Type of event

type

libevent

libev

libuv

IO

fd

io

fs_event

Timer ( mono clock )

timer

timer

timter

Timer ( wall clock )

-

periodic

-

signal

signal

signal

signal

Process control

-

child

process

File stat

-

stat

fs_poll

Idle event that will be executed every cycle

-

idle

idle

Execute before loop block

-

prepare

prepare

Execute after loop blcck

-

check

check

Nested loop

-

embed

-

fork

-

fork

-

Cleanup before loop destruction

-

cleanup

-

Operate the loop in another thread

-

async

async

stream (tcp, pipe, tty)

stream (tcp, pipe, tty)

stream (tcp, pipe, tty)

stream (tcp, pipe, tty)


This comparison is more meaningful for libev and libuv. For libevent, a lot of it is related to its design philosophy. Embed in libev is rarely used, and it doesn't matter if libuv is not available; cleanup can be replaced by async_exit in libuv; libuv has no fork event.


portability

All three libraries support Linux, *BSD, Mac OS X, Solaris, Windows

type

libevent

libev

libuv

dev/poll (Solaris)

y

y

y

event ports

y

y

y

kqueue (*BSD)

y

y

y

POSIX select

y

y

y

Windows select

y

y

y

Windows IOCP

y

N

y

poll

y

y

y

epoll

y

y

y


For Unix/Linux platforms, there is no big difference. Choose epoll first. For windows, libevent and libev both use select to detect and distribute events (not I/O), and libuv uses IOCP under windows. Libevent has a socket handle, which uses IOCP to read and write on Windows. There is no similar for libev. But libevent's IOCP support is not very good (performance is not high). So if it is under the windows platform, use the native IOCP for I/O, or use libuv.


Principles of Asynchronous Architecture Programming

1. The callback function cannot be executed for a long time, because a loop may contain other events, especially some timers with higher accuracy requirements.

2. Try to use the time cached in the library, sometimes you need to perform operations such as timeout based on the time difference. Of course, it is best to use the timer in the library.


Reference : https://blog.csdn.net/qq_42211289/article/details/80366698