libevent基础知识

在线手册


• 官方网站:http://libevent.org/

• 官方手册:http://www.wangafu.net/~nickm/libevent-book/

• 官方下载:http://sourceforge.net/projects/levent/files/libevent/

• 在线手册:http://www.monkey.org/~provos/libevent/doxygen-2.0.1/index.html

       http://www.wangafu.net/~nickm/libevent-2.0/doxygen/html/index.html

• 百度百科:http://baike.baidu.com/link?url=cAxwbS2-agM4LSIYDQS1tAFxYrkLjju-6gcARq2iG6keI6MP6_WZpPCvZWKgAJbFnHNL40u5yYz6qhb71RQg4K

• 维基百科:https://en.wikipedia.org/wiki/Libevent

编译和安装(Linux : CentOS 7.0)


下载并解压libevent压缩包

chmod 777 ./configure

./configure -prefix=/usr

make

make install

检测libevent是否安装成功:ls -al /usr/lib | grep libevent

 测试示例


1. 获取版本信息

// test.cpp
#include
<stdio.h> #include <event.h> int main(int argc, char ** argv) { const char *cszVersion = event_get_version(); printf("Version : %s\n", cszVersion); return 0; }

g++ -o test.o -c test.cpp

g++ -levent -o test test.o

2. 简单触发器

// test.cpp
#include
<stdio.h> #include <event.h> #include <iostream> void OnTime(evutil_socket_t fd, short event, void *arg) { std::cout << "OnTime Triggers!" << std::endl; struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; event_add((struct event*)arg, &tv); } int main(int argc, char ** argv) { struct timeval tv; struct event evTime; tv.tv_sec = 1; tv.tv_usec = 0; event_init(); evtimer_set(&evTime, OnTime, &evTime); event_add(&evTime, &tv); event_dispatch(); return 0; }

g++ -o test.o -c test.cpp

g++ -levent -o test test.o

posted @ 2015-08-31 17:56  碎语心弦  阅读(388)  评论(0编辑  收藏  举报