All pastes #477606 Raw Edit

Mine

public text v1 · immutable
·published 2007-05-08 19:13 UTC
rendered paste body
void SocketServer::serve()
{
    while (!m_done)
    {
        // create slave socket
        SOCKET slaveSocket = accept(...);

         // spin until some requests get processed
        while (enqueue(slaveSocket))
            boost::thread::yield();
    }
}

void SocketServer::processQueue()
{
    for (;;)
    {
        Request request = dequeue();
        int socket = request.socket;
        if (!valid_socket(socket))
            break;
        serve(socket);
    }
}

int SocketServer::enqueue(SocketServer::Request request)
{
    boost::mutex::scoped_lock lock(m_queueMutex);

    if (m_queue.size() >= m_queueSize)
        return -1;
    m_queue.push(request);
    m_queueCond.notify_one();

    return 1;
}

SocketServer::Request SocketServer::dequeue()
{
    boost::mutex::scoped_lock lock(m_queueMutex);

    while (m_queue.empty())
        m_queueCond.wait(lock);
    SocketServer::Request request = m_queue.front();
    m_queue.pop();

    return request;
}