diff --git a/src/observer/CMakeLists.txt b/src/observer/CMakeLists.txt index 1e156f2..37f5f86 100644 --- a/src/observer/CMakeLists.txt +++ b/src/observer/CMakeLists.txt @@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC}) ENDFOREACH (F) -SET(LIBRARIES common pthread dl event jsoncpp) +SET(LIBRARIES common pthread dl event_pthreads event jsoncpp) # 指定目标文件位置 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin) diff --git a/src/observer/main.cpp b/src/observer/main.cpp index 4fff6da..112255e 100644 --- a/src/observer/main.cpp +++ b/src/observer/main.cpp @@ -137,8 +137,6 @@ void *quit_thread_func(void *_signum) LOG_INFO("Receive signal: %ld", signum); if (g_server) { g_server->shutdown(); - delete g_server; - g_server = nullptr; } return nullptr; } @@ -169,4 +167,6 @@ int main(int argc, char **argv) LOG_INFO("Server stopped"); cleanup(); + + delete g_server; } diff --git a/src/observer/net/server.cpp b/src/observer/net/server.cpp index e87ad0d..2c6cef6 100644 --- a/src/observer/net/server.cpp +++ b/src/observer/net/server.cpp @@ -26,6 +26,7 @@ See the Mulan PSL v2 for more details. */ #include #include #include +#include #include "common/lang/mutex.h" #include "common/log/log.h" @@ -411,6 +412,7 @@ int Server::start_unix_socket_server() int Server::serve() { + evthread_use_pthreads(); event_base_ = event_base_new(); if (event_base_ == nullptr) { LOG_ERROR("Failed to create event base, %s.", strerror(errno)); @@ -425,19 +427,6 @@ int Server::serve() event_base_dispatch(event_base_); - return 0; -} - -void Server::shutdown() -{ - LOG_INFO("Server shutting down"); - - // cleanup - struct timeval exit_time; - gettimeofday(&exit_time, nullptr); - exit_time.tv_sec += 10; - event_base_loopexit(event_base_, &exit_time); - if (listen_ev_ != nullptr) { event_del(listen_ev_); event_free(listen_ev_); @@ -451,4 +440,16 @@ void Server::shutdown() started_ = false; LOG_INFO("Server quit"); + return 0; +} + +void Server::shutdown() +{ + LOG_INFO("Server shutting down"); + + // cleanup + if (event_base_ != nullptr && started_) { + started_ = false; + event_base_loopexit(event_base_, nullptr); + } }