avoid crashing in the multi-thread operation for std::vector

1. stl container is not thread safety
2. rm_fd() and loop() end to clear the retired event source will in multi-thread operation
3. may be crashed in reaper thread to delete the items in the std::vector as the source is nullptr
This commit is contained in:
Justin.Hung 2016-12-02 17:33:13 +08:00
parent 9a20f42f62
commit e1368bdac8
2 changed files with 8 additions and 0 deletions

View File

@ -92,7 +92,9 @@ void zmq::epoll_t::rm_fd (handle_t handle_)
int rc = epoll_ctl (epoll_fd, EPOLL_CTL_DEL, pe->fd, &pe->ev);
errno_assert (rc != -1);
pe->fd = retired_fd;
retired_sync.lock ();
retired.push_back (pe);
retired_sync.unlock ();
// Decrease the load metric of the thread.
adjust_load (-1);
@ -180,10 +182,12 @@ void zmq::epoll_t::loop ()
}
// Destroy retired event sources.
retired_sync.lock ();
for (retired_t::iterator it = retired.begin (); it != retired.end (); ++it) {
LIBZMQ_DELETE(*it);
}
retired.clear ();
retired_sync.unlock ();
}
}

View File

@ -41,6 +41,7 @@
#include "fd.hpp"
#include "thread.hpp"
#include "poller_base.hpp"
#include "mutex.h"
namespace zmq
{
@ -102,6 +103,9 @@ namespace zmq
// Handle of the physical thread doing the I/O work.
thread_t worker;
// Synchronisation of the retired event source
mutex_t retired_sync;
epoll_t (const epoll_t&);
const epoll_t &operator = (const epoll_t&);
};