Commit 040a0e9b authored by captainwong's avatar captainwong

fix server sometimes cannot exit

parent 02a6efaf
...@@ -303,16 +303,19 @@ struct simple_libevent_server::PrivateImpl ...@@ -303,16 +303,19 @@ struct simple_libevent_server::PrivateImpl
simple_libevent_server::simple_libevent_server() simple_libevent_server::simple_libevent_server()
{ {
AUTO_LOG_FUNCTION;
SIMPLE_LIBEVENT_ONE_TIME_INITTER; SIMPLE_LIBEVENT_ONE_TIME_INITTER;
} }
simple_libevent_server::~simple_libevent_server() simple_libevent_server::~simple_libevent_server()
{ {
AUTO_LOG_FUNCTION;
stop(); stop();
} }
bool simple_libevent_server::start(uint16_t port, std::string& msg) bool simple_libevent_server::start(uint16_t port, std::string& msg)
{ {
AUTO_LOG_FUNCTION;
do { do {
stop(); stop();
...@@ -384,11 +387,13 @@ bool simple_libevent_server::start(uint16_t port, std::string& msg) ...@@ -384,11 +387,13 @@ bool simple_libevent_server::start(uint16_t port, std::string& msg)
void simple_libevent_server::stop() void simple_libevent_server::stop()
{ {
AUTO_LOG_FUNCTION;
std::lock_guard<std::mutex> lg(mutex); std::lock_guard<std::mutex> lg(mutex);
if (!impl) { return; } if (!impl) { return; }
if (impl->base) {
timeval tv{ 0, 1000 }; timeval tv{ 0, 1000 };
if (impl->base) {
event_base_loopexit(impl->base, &tv); event_base_loopexit(impl->base, &tv);
} }
...@@ -402,7 +407,7 @@ void simple_libevent_server::stop() ...@@ -402,7 +407,7 @@ void simple_libevent_server::stop()
} }
for (int i = 0; i < threadNum_; i++) { for (int i = 0; i < threadNum_; i++) {
event_base_loopexit(impl->workerThreadContexts[i]->base, nullptr); event_base_loopexit(impl->workerThreadContexts[i]->base, &tv);
} }
for (int i = 0; i < threadNum_; i++) { for (int i = 0; i < threadNum_; i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment