Skip to content

Instantly share code, notes, and snippets.

@baixiangcpp
Created August 31, 2018 16:17
Show Gist options
  • Select an option

  • Save baixiangcpp/ae836f31e618d002e6611ad0006c5c74 to your computer and use it in GitHub Desktop.

Select an option

Save baixiangcpp/ae836f31e618d002e6611ad0006c5c74 to your computer and use it in GitHub Desktop.
A libevent helloworld using changelist
#include <event2/event.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define BUF_SIZE 1024
void onError(const char *reason)
{
perror(reason);
exit(EXIT_FAILURE);
}
void read_cb(int fd,short events,void* arg)
{
char buf[BUF_SIZE] = {0};
int ret = read(fd,buf,sizeof(buf)-1);
if(ret < 0 )
onError("read()");
buf[ret] = 0;
printf("What you type is : %s \n",buf);
}
int main()
{
struct event_config *cfg = event_config_new();
event_config_set_flag(cfg,EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST);
struct event_base* base = event_base_new_with_config(cfg);
if(!base)
onError("event_base_new()");
struct event* ev = event_new(base,STDIN_FILENO,EV_READ | EV_PERSIST ,read_cb,NULL);
if(!ev)
onError("event_new()");
event_add(ev,NULL);
event_base_dispatch(base);
event_free(ev);
event_base_free(base);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment