There was a previous article that described some contrasts:
Http://www.cnblogs.com/charlesblc/p/6078029.html
Here's a bit more about code and application.
Look at the two sides of the HelloWorld basic understanding of the approximate.
Libevent's Hello World
#include <sys/signal.h>#include<event.h>voidSIGNAL_CB (intFd ShortEventvoid*Arg) {Event_base*base = (event_base*) Arg; Timeval Delay= {2, 0}; printf ("Caught an interrupt signal, exit in 2 sec ... \ n "); Event_base_loopexit (Base,&delay);}voidTIMEOUT_CB (intFd ShortEventvoid*Arg) {printf ("Timeout\n");}intMain () { event_base *base = event_init (); Event*signal_event =evsignal_new (Base, SIGINT, SIGNAL_CB, base); Event_add (Signal_event, NULL); Timeval TV= {1, 0}; Event* Timeout_event =evtimer_new (Base, TIMEOUT_CB, NULL); Event_add (Timeout_event,&TV); Event_base_dispatch (base); Event_free (timeout_event); Event_free (signal_event); Event_base_free (base);}
Above libevent the most basic event drive is event_base *base = Event_init (); Below Libev the most basic event drive is the struct ev_loop *main_loop = ev_default_loop (0);
The above Libevent event, at initialization time, will be added to the drive, event *signal_event = Evsignal_new (base, SIGINT, SIGNAL_CB, base); Then direct event_add on the line, this time without adding a drive;
The following Libev things, at the time of initialization, do not involve drives, relate events and callback functions first: Ev_init (&io_w,io_action); Rebind Event Source: Ev_io_set (&io_w,stdin_fileno,ev_read); Finally, register to the drive: Ev_io_start (MAIN_LOOP,&IO_W);
Above libevent start event loop with Event_base_dispatch (base); The following Libev begins the event loop with Ev_run (main_loop,0);
Libev's Hello World
#include <ev.h>#include<stdio.h>#include<signal.h>#include<sys/unistd.h>ev_io io_w;ev_timer timer_w;ev_signal signal_w;voidIo_action (struct Ev_loop *main_loop,ev_io *io_w,inte) { intrst; CharBUF[1024] = {'} '}; Puts ("In Io cb\n"); Read (stdin_fileno,buf,sizeof (BUF)); buf[1023] = ' + '; printf ("Read in a string%s \ n", BUF); Ev_io_stop (Main_loop,io_w);}voidTimer_action (struct Ev_loop *main_loop,ev_timer *timer_w,inte) {Puts ("In Tiemr CB \ n"); Ev_timer_stop (Main_loop,timer_w);}voidSignal_action (struct Ev_loop *main_loop,ev_signal Signal_w,inte) {Puts ("In signal CB \ n"); Ev_signal_stop (Main_loop,signal_w); Ev_break (Main_loop,evbreak_all);}intMainintARGC,Char*argv[]) { struct ev_loop *main_loop = ev_default_loop (0); Ev_init (&io_w,io_action); Ev_io_set (&io_w,stdin_fileno,ev_read); Ev_init (&timer_w,timer_action); Ev_timer_set (&timer_w,2,0); Ev_init (&signal_w,signal_action); Ev_signal_set (&signal_w,sigint); Ev_io_start (Main_loop,&io_w); Ev_timer_start (Main_loop,&Timer_w); Ev_signal_start (Main_loop,&Signal_w); Ev_run (Main_loop, 0);return0;}
Comparison of the difference between Libevent and Libev (II.)