Код: Выделить всё
class Socket
{
[...]
pollfd* fdPtr;
}
Когда создается (принимается (принимается), FDPTR установлен:
Код: Выделить всё
class Manager
{
std::vector
pollFdsVec
std::vector socketsVec;
void Accept()
{
// When a new connection is created
std::shared_ptr sock = Accept(...); // listener returns a sharedptr
socketsVec.push_back(sock);
// Add the new connection to the pfds
pollfd newPfd;
[...]
m_pollFdsVec.push_back(newPfd);
// Set the pointer
sock->fdPtr = &pollFdsVec[pollFdsVec.size() - 1]
}
void Poll()
{
int res = poll(pollFdsVec);
[...]
}
};
Код: Выделить всё
if (toRemove.size() > 0)
{
// Remove socket from list and fds, in reverse order to avoid index shift
std::sort(toRemove.begin(), toRemove.end(), std::greater());
for (int idx : toRemove)
{
std::swap(pollFdsVec[idx], pollFdsVec.back());
std::swap(socketsVec[idx], socketsVec.back());
// Update the socket that was sitting at the back and suffered the swap
socketsVec[idx]->SetPfd(&m_poll_fds[idx]);
m_poll_fds.pop_back();
m_list.pop_back();
}
}
Этот цикл объединения случается в одном потоке, и да, я нуждаюсь в том, чтобы узнать, что я могу подать в опрос. События.>
Подробнее здесь: https://stackoverflow.com/questions/797 ... ers-memory