-1

I have 2 threads:

Thread A: It's the select() loop. Perform socket handling for reading operations, such as accepting new connections, receiving data.

while (1) {

    FD_ZERO(&fdReadSet);

    numActiveSockets = 0;

    for (std::unordered_map<SOCKET, TcpSocket*>::iterator it = m_sock_table.begin(); it != m_sock_table.end(); it++)
    {
        numActiveSockets++;
        FD_SET(it->first, &fdReadSet);
    }

    int ret;
    bool hasListen = false;

    if (( ret = select(numActiveSockets, &fdReadSet, NULL, NULL, NULL)) == SOCKET_ERROR) {
        printf("Select Failed, Error code = %d\n", WSAGetLastError());
        return -1;
    }

    for (std::unordered_map<SOCKET, TcpSocket*>::iterator it = m_sock_table.begin(); it != m_sock_table.end(); it++)
    {
        if (FD_ISSET(it->first, &fdReadSet))
        {
            if (it->first == TcpSocket::m_listen_sock)
            {
                if (!hasListen)
                {
                    sockaddr_in sock_addr;
                    int sockLength = sizeof(sock_addr);

                    SOCKET sock = accept(it->first, (sockaddr *) &sock_addr, &sockLength);
                    TcpSocket * socket = new TcpSocket();
                    socket->m_sock = sock;
                    m_sock_table[sock] = socket;

                    it = m_sock_table.begin();
                    hasListen = true;
                }
            }
            else
            {
                char * buffer = it->second->GetWriteBuffer();
                int numRead = recv(it->first, buffer, SOCKET_BUFFER_SIZE, 0);

                if (numRead == SOCKET_ERROR)
                {
                    int err = WSAGetLastError();
                    if (err == WSAECONNRESET)
                    {
                        printf("Connection [%i]: RESET Received. Closing Socket\n", it->first);
                        closesocket(it->first);
                        it = socketVector.erase(it->first);  // iterator invalidated after erase
                    }
                    else
                    {
                        printf("Recv Failed. Error code = %d\n", err);
                        return -1;
                    }
                }
                else if (numRead == 0)//connection close
                { 
                    printf("Connection [%i]: Graceful exit. Closing Socket\n", it->first);
                    closesocket(it->first);
                    it = socketVector.erase(it->first);  // iterator invalidated after erase

                }
                else {
                    /* Process received data */

                }

            }
        }
    }
}

Thread B: Allow the application to perform connect() to establish new connections. If a connect() is successful, it will the add the returned socket to m_sock_table.

I have a socket table called m_sock_table which holds all the sockets. I use this m_sock_table to initialize the fdReadSet to be used in select().

-----------Problem-----------------

If thread A is blocked by select(), and at the same time thread B establish a new connection through connect(), the application wouldn't be able to receive data from the new connection, because fdReadset has not been updated withed the new connected socket.

What would be a good way to solve this problem? Or the design is just wrong from the start?

4

1 回答 1

1

您可以使用除了中断系统调用之外什么都不做的信号:

#include <signal.h>
void do_nothing() { }

struct sigaction sa;
sa.sa_handler = do_nothing;
sigemptyset(sa.sa_mask);
#ifdef SA_INTERRUPT
sa.sa_flags = SA_INTERRUPT;
#else
sa.sa_flags = 0;
#endif
sigaction(SIGUSR1, &sa, 0);

然后,在线程 B 中,在启动新连接后,发送信号,确保线程 A 将处理它:

/* need only be done once, but needed in every thread other than A */
sigset_t sigs;
sigemptyset(&sigs);
sigaddset(&sigs, SIGUSR1)
pthread_sigmask(SIG_BLOCK, &sigs, 0);

/* each time we create a new connection */
kill(getpid, SIGUSR1);

使用上述方法,select 将返回一个 EINTR 错误——因此检查并循环(将新连接添加到集合中)。

于 2013-10-06T20:12:22.770 回答