[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] mixing libevent and boost::asio



On Thu, Nov 20, 2014 at 10:44:40AM +0100, svante karlsson wrote:
> Is there a good way of interacting between those two libraries?
> 
> I'm trying to mix two single threaded libraries - one built for each
> platform. It's simple to poll each library but this this seems to mean
> either introducing a sleep in the loop of spinning a full speed.
> 
> a naive approach...
> ....
>   while (true)
>     {
>        // does not block but might handle several events
>        int s1 = event_base_loop(_event_base, EVLOOP_NONBLOCK);

Not always, it could wait in case there are no events ready to trigger.
(according to sources, but maybe I missed something, Nick could you
verify?)

> 
>        // handles at most 1 event
>       size_t n = ios.poll();

Same here.

>        if (n>0)
>            continue;
>        boost::this_thread::sleep(boost::posix_time::milliseconds(1));
>     }
> 
> Is there a way of waiting for both sets of file descriptors so I don't have
> to rely on the timer or a busy loop to poll both events?

In theory (for linux only) event_base_loop/ios.poll is epoll_wait, and
in epoll case you could add epoll fd into epoll, IOW something like this
will work:

efd1 = epoll_create(); # this is you libevent loop
efd2 = epoll_create(); # this is you asio loop
efd3 = epoll_create(); # this is you "aggregation" loop
epoll_ctl(efd3, EPOLL_CTL_ADD, efd1, ...);
epoll_ctl(efd3, EPOLL_CTL_ADD, efd2, ...);

But:
- this will work only for linux (I don't sure about select/poll/kqueue/iocp)
- libevent don't export epoll fd
- I don't think that asio do export it
- and personally I don't think that it is sane to use two event loops in one application

Cheers,
Azat.

> I know I can have one thread running the libevent queue and another the
> boost::asio eventloop but that gives problems when the callbacks in each
> subsystem calls the other.
> 
> I'm familiar with boost but not libevent so please bear with me if this
> question is trivial in libevent context...
> 
> /svante
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.