So, is it thread-safety **work:

1) On startup I call to event_init() which give me back an "event base"

bool SocketManager::Init() {
    m_base = (event_base*)event_init();
    return (m_base != NULL);
}

2) I run libevent mainloop in new thread with this "event base"
void SocketRunnable::ThreadProc() {
  //
    while(!g_bExit)
    {
       //
       // ....
       //
        if( event_base_loop(m_base, 0) == -1 )
        {
                outError("<Socket manager> Critical error is occured in
event_loot. Exit.");
                return;
        }
    }
}

This thread handles the listen socket; it calls accept() and
SocketManager::AddSocket() to add new socket event loop

bool SocketManager::AddSocket(SocketBase* pSocket)
{
    event_set(&pSocket->m_ev_read, (int)pSocket->GetHandle(), EV_READ |
EV_PERSIST, io_handle_read, pSocket);
    event_base_set(m_base, &pSocket->m_ev_read);

    if(event_add(&pSocket->m_ev_read, &tv) == -1)
        return false;

    event_set(&pSocket->m_ev_write, (int)pSocket->GetHandle(), EV_WRITE,
io_handle_write, pSocket);
    event_base_set(m_base, &pSocket->m_ev_write);

    return true;
}


3) After "event_base_set(m_base, &pSocket->m_ev_write);"
if i want to send from another thread, i can call smth like this:

bool Socket::Send(IN const uint8* pSrc, INOUT uint32 unSize)
{
    //some checks and output buffer operation
    return (event_add(&m_ev_write, NULL) != -1);
}

and on write callback i send data directly in socket:

void io_handle_write(int fd, short event, void *arg)
{
    send(fd, ........);
}

am i right or wrong?

Thanks.

P.S. Sorry for bad English..

Best regards,
Maksim Lapo
_______________________________________________
Libevent-users mailing list
Libevent-users@monkey.org
http://monkeymail.org/mailman/listinfo/libevent-users

Reply via email to