我的环境如下:
libevent2.1.8
Windows7系统
问题描述:
使用event_base_loopbreak或event_base_loopexit无法让event_base_dispatch退出事件循环
原因及解决方案:
经过一天的折腾,发现是多线程环境下没有调用evthread_use_windows_threads或evthread_use_threads函数导致event_base_dispatch函数一直阻塞,即使调用了event_base_loopbreak或event_base_loopexit也无法让event_base_dispatch退出事件循环。
代码描述:
一个线程调用startServer函数启动监听服务,另外一个线程调用stopServer函数停止服务
主要代码如下:
int CHttpServer::startServer()
{
u_short port = m_serverPort;
struct sockaddr_in listen_addr;
// struct event ev_accept;
int reuseaddr_on;
/* Initialize libevent. */
event_init();
#ifdef __unix
/* Set signal handlers */
sigset_t sigset;
sigemptyset(&sigset);
struct sigaction siginfo = {
.sa_handler = sighandler,
.sa_mask = sigset,
.sa_flags = SA_RESTART,
};
sigaction(SIGINT, &siginfo, NULL);
sigaction(SIGTERM, &siginfo, NULL);
#endif
/* Create our listening socket. */
m_listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (m_listenfd < 0)
{
#ifdef __unix
err(1, "listen failed");
#endif
}
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_addr.s_addr = INADDR_ANY;
listen_addr.sin_port = htons(port);
if (bind(m_listenfd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) {
#ifdef __unix
err(1, "bind failed");
#endif
}
if (listen(m_listenfd, CONNECTION_BACKLOG) < 0) {
#ifdef __unix
err(1, "listen failed");
#endif
}
reuseaddr_on = 1;
setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuseaddr_on, sizeof(reuseaddr_on));
/* Set the socket to non-blocking, this is essential in event
* based programming with libevent. */
evutil_make_socket_nonblocking(m_listenfd);//设置非阻塞socket
// if (setnonblock(m_listenfd) < 0) {
//#ifdef __unix
// err(1, "failed to set server socket to non-blocking");
//#endif
// }
const char** all_methods = event_get_supported_methods();
while( all_methods && *all_methods )
{
printf("%s\n", *all_methods++);
}
#ifdef _WIN32
//告诉libEvent使用Windows线程
//这句是必须的,不然会导致event_base_dispatch时一直处于Sleep状态,无法正常工作
// evthread_use_windows_threads();
//支持多线程,需要线程安全,该函数一定要在event_base_new之前调用,如果在event_base_new之后才调用的话,那么该event_base就不会是线程安全的了
evthread_use_windows_threads();
struct event_config* cfg = event_config_new();
event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP);
//根据CPU实际数量配置libEvent的CPU数
SYSTEM_INFO si;
GetSystemInfo(&si);
event_config_set_num_cpus_hint(cfg,si.dwNumberOfProcessors);
if ((m_pEvbase_accept = event_base_new_with_config(cfg)) == NULL)
{
event_config_free(cfg);
perror("Unable to create socket accept event base");
close(m_listenfd);
m_listenfd = -1;
return 1;
}
event_config_free(cfg);
#else
if ((m_pEvbase_accept = event_base_new()) == NULL)
{
perror("Unable to create socket accept event base");
close(m_listenfd);
return 1;
}
#endif
const char *curmethod = event_base_get_method(m_pEvbase_accept);
printf("current method:\t %s\n",curmethod);
// int event_base_get_features(m_pEvbase_accept);
// static int event_config_is_avoided_method(const struct event_config *cfg, const char *method);
/* Initialize work queue. */
if (CWorkQueue::workqueue_init(&m_workqueue, NUM_THREADS))
{
perror("Failed to create work queue");
close(m_listenfd);
m_listenfd = -1;
CWorkQueue::workqueue_shutdown(&m_workqueue);
return 1;
}
/* We now have a listening socket, we create a read event to
* be notified when a client connects. */
event_set(&m_ev_accept, m_listenfd, EV_READ|EV_PERSIST, on_accept, (void *)&m_workqueue);
event_base_set(m_pEvbase_accept, &m_ev_accept);
event_add(&m_ev_accept, NULL);
m_isRuning = true;
printf("Server running.\n");
/* Start the event loop. */
event_base_dispatch(m_pEvbase_accept);
// event_base_loop(m_pEvbase_accept, EVLOOP_NO_EXIT_ON_EMPTY);
event_free(&m_ev_accept);
event_base_free(m_pEvbase_accept);
m_pEvbase_accept = NULL;
close(m_listenfd);
m_listenfd = -1;
printf("Server shutdown.\n");
return 0;
}
int CHttpServer::stopServer()
{
if(!runing()) return SE_STOPPED;
fprintf(stdout, "Stopping socket listener event loop.\n");
// event_base_loopexit()让event_base在给定时间之后停止循环。如果tv参数为NULL,event_base会立即停止循环,没有延时。如果event_base当前正在执行任何激活事件的回调,则回调会继续运行,直到运行完所有激活事件的回调之才退出。
// event_base_loopbreak()让event_base立即退出循环。它与event_base_loopexit(base,NULL)的不同在于,如果event_base当前正在执行激活事件的回调,它将在执行完当前正在处理的事件后立即退出。
//以下两个函数调用后,监听事件循环还是阻塞的。。。。
if (event_base_loopbreak(m_pEvbase_accept))
{
perror("Error shutting down server");
}
if (event_base_loopexit(m_pEvbase_accept,NULL))
{
perror("Error shutting down server");
}
// event_active(&m_ev_accept, EV_READ|EV_PERSIST, 1);
// close(m_listenfd);
fprintf(stdout, "Stopping workers.\n");
CWorkQueue::workqueue_shutdown(&m_workqueue);
if(NULL != m_pCHttpListenThread)
{
delete m_pCHttpListenThread;
m_pCHttpListenThread = NULL;
}
m_isRuning = false;
// LOGGER_CINFO(theLogger, __TEXT("Q++ HTTP Server 停止.\r\n"));
return SE_SUCCESS;
}