libevent使用event_new和不使用的两种方法
2024-09-20 16:01:13
写两个简单的demo,对照一下各自的方法
#include <sys/types.h> #include <event2/event-config.h> #include <sys/stat.h>
#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event.h>
#include <event2/event.h> int called = ; static void
signal_cb(evutil_socket_t fd, short event, void *arg)
{
struct event *signal = arg; printf("signal_cb: got signal %d\n", event_get_signal(signal)); if (called >= )
event_del(signal); called++;
} int
main(int argc, char **argv)
{
struct event signal_int;
struct event_base* base;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData; wVersionRequested = MAKEWORD(, ); (void) WSAStartup(wVersionRequested, &wsaData);
#endif /* Initalize the event library */
// base = event_base_new();
//
// /* Initalize one event */
// signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg());
event_init();
event_set(&signal_int, SIGINT, EV_SIGNAL, signal_cb, (void *)&signal_int);
event_add(&signal_int, NULL); event_dispatch();
//event_free(signal_int);
//event_base_free(base); return ();
}
#include <sys/types.h> #include <event2/event-config.h> #include <sys/stat.h>
#ifndef _WIN32
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
#else
#include <winsock2.h>
#include <windows.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h> #include <event2/event.h> int called = 0; static void
signal_cb(evutil_socket_t fd, short event, void *arg)
{
struct event *signal = arg; printf("signal_cb: got signal %d\n", event_get_signal(signal)); if (called >= 2)
event_del(signal); called++;
} int
main(int argc, char **argv)
{
struct event *signal_int;
struct event_base* base;
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); (void) WSAStartup(wVersionRequested, &wsaData);
#endif /* Initalize the event library */
base = event_base_new(); /* Initalize one event */
signal_int = evsignal_new(base, SIGINT, signal_cb, event_self_cbarg()); //evsignal_new内部调用了event_new
event_add(signal_int, NULL);
event_base_dispatch(base);
event_free(signal_int);
event_base_free(base); return (0);
}
最新文章
- Java 引用分类:StrongReference、SoftReference、WeakReference、PhantomReference
- MyBank(自助银行)系统
- 求出数组前面k个元素或数组中元素大于一半的元素(快速排序与堆排序的灵活运用)
- LeetCode题解——Add Two Numbers
- Graph(2014辽宁ACM省赛)
- Linux下搭建Oracle11g RAC(5)----配置ASM磁盘
- xml代码
- hibernate在地图的方法之一协会
- 1572: [Usaco2009 Open]工作安排Job[贪心]
- 监控利器 sysdig - 每天5分钟玩转 Docker 容器技术(79)
- Aurora 论坛图片下载
- python scrapy框架爬虫遇到301
- docker镜像常用操作
- PDFBOX详解
- Oracle EBS FA 资产取值
- jsp button onclick
- 基于MSAA的QQ界面信息获取的实现
- APP消息推送机制的实现(PUSH)
- C++代码统计工具
- orcale_proceduie_function_两三栗