多线程服务器不在线程cpp之间切换

Multithreaded server don't switch between threads cpp

我正在尝试制作一个多线程服务器,但由于某种原因,我的服务器的线程没有切换。只有最后创建的线程是 运行,其他线程不是 运行。

这是主服务器的代码:

void Server::serve(int port)
{

    struct sockaddr_in sa = { 0 };

    sa.sin_port = htons(port); // port that server will listen for
    sa.sin_family = AF_INET;   // must be AF_INET
    sa.sin_addr.s_addr = INADDR_ANY;    // when there are few ip's for the machine. We will use always "INADDR_ANY"

    // Connects between the socket and the configuration (port and etc..)
    if (bind(_serverSocket, (struct sockaddr*)&sa, sizeof(sa)) == SOCKET_ERROR)
        throw std::exception(__FUNCTION__ " - bind");

    std::cout << "binded" << std::endl;

    // Start listening for incoming requests of clients
    if (listen(_serverSocket, SOMAXCONN) == SOCKET_ERROR)
        throw std::exception(__FUNCTION__ " - listen");
    std::cout << "Listening on port " << port << std::endl;

    while (true)
    {
        SOCKET client_socket = accept(_serverSocket, NULL, NULL);
        if (client_socket == INVALID_SOCKET)
            throw std::exception(__FUNCTION__);

        std::cout << "Accepting clients..." << std::endl;
        std::cout << "Client accepted" << std::endl;

        std::thread newClientThread(&Server::clientThread, this, std::ref(client_socket));  // Make thread of client
        newClientThread.detach();
    }
}

这是客户端的线程:

void Server::clientThread(SOCKET& clientSocket)
{
    Helper ourHelper;
    std::string msg = "";
    int lengthOfMessage = 0;
    this->_vectorOfSockets.push_back(clientSocket);
    try
    {
        while (true)
        {
            std::cout << clientSocket;
            /*
            // Get message and save into queue
            msg = ourHelper.getStringPartFromSocket(clientSocket, 1024);
            msg = this->returnFullMsg(msg);
            try
            {
                std::cout << clientSocket << " - d\n";
                std::lock_guard<std::mutex> lck(mtx);
                std::cout << clientSocket << " - d\n";
                this->_messagesQueue.push(msg);
                ourConditionVariable.notify_one();
                std::this_thread::sleep_for(std::chrono::seconds(1));  // Wait the main thread to take care for the message
            }
            catch (std::exception e)
            {

            }*/
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    }
    catch (const std::exception& e)
    {
        std::cout << "Client logged out\n";
        // this->_users.erase(msg.substr(5, atoi(msg.substr(3, 5).c_str() + 5)));  // Remove the user from the connected users list
        closesocket(clientSocket);
    }
}

这是主线程的代码:

int main()
{
    Server myServer;
    std::string newMessage;
    std::thread ourConnectorThread (connectorThread, std::ref(myServer));
    ourConnectorThread.join();
    /*std::cout << "Starting...\n";
    while (true)
    {
        std::unique_lock<std::mutex> lckMessages(ourMutex);
        ourConditionVariable.wait(lckMessages);  // Wait for new message
        newMessage = myServer.getQueue().front();  // Get the new message.
        myServer.getQueue().pop();  // Remove the first item
        takeCareForMessage(newMessage, myServer);
        lckMessages.unlock();
    }*/
    return 0;
}

评论中的代码无关紧要。 这段代码的结果是最后一个线程每秒只打印套接字的数量,而其他线程没有打印任何东西。 我的代码有什么问题?

代码中的一个主要错误是 client_socket 通过引用传递,然后由服务器线程修改。解决方法是按值传递它。

另一个错误是 _vectorOfSockets.push_back 被多个线程修改 - 竞争条件。您需要使用互斥锁来解决这个问题。

accept 可能会在客户端断开连接时失败。那不是不可恢复的异常情况,不需要抛出异常,重试accept即可恢复。