libevent signal 信号事件

关于libevent信号事件(SIGINT信号)的一个小列子

#include 
#include 
#include 
#include 
#include 
using namespace std;

struct event_base *base;

int called = 0;

void signal_cb(evutil_socket_t fd,short event,void *arg){
	struct event *signal = (struct event *)arg;
	printf("%s : got signal %d\n",__func__,event_get_signal(signal));
	if(called >= 2)
		event_del(signal); //删除信号事件
	called++;
//	event_base_loopbreak(base);
}

int main(int argc, char **argv){
	base = event_base_new();
	if(base == NULL){
		printf("create event base failed \n");
		return -1;
	}
	struct event *signal_int = evsignal_new(base,SIGINT,signal_cb,event_self_cbarg()); //
	if(!signal_int || event_add(signal_int,NULL) < 0){
		printf("create or add signal_int failed \n");
		return -1;
	}
	event_base_dispatch(base);		
	event_base_free(base);
	printf("after event_base_dispatch\n");
	return 0;
}


参考:https://github.com/libevent/libevent/blob/master/sample/signal-test.c




 
 

你可能感兴趣的:(C++)