Skip to content

Commit

Permalink
Avoid event poller thread join and delete itself
Browse files Browse the repository at this point in the history
  • Loading branch information
xia-chu committed Feb 7, 2024
1 parent f301583 commit fca6d23
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ static SockException getSockErr(int sock, bool try_errno = true) {

Socket::Ptr Socket::createSocket(const EventPoller::Ptr &poller_in, bool enable_mutex) {
auto poller = poller_in ? poller_in : EventPollerPool::Instance().getPoller();
return Socket::Ptr(new Socket(poller, enable_mutex), [poller](Socket *ptr) { poller->async([ptr]() { delete ptr; }); });
std::weak_ptr<EventPoller> weak_poller = poller;
return Socket::Ptr(new Socket(poller, enable_mutex), [weak_poller](Socket *ptr) {
if (auto poller = weak_poller.lock()) {
poller->async([ptr]() { delete ptr; });
} else {
delete ptr;
}
});
}

Socket::Socket(EventPoller::Ptr poller, bool enable_mutex)
Expand Down

0 comments on commit fca6d23

Please sign in to comment.