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

Re: [Libevent-users] Wait until the loop thread processes all the data?



I managed to implement a reliable solution as follows: instead of
using a socket pair and a pair of socket bufferevents, I used "paired
bufferevents", which support flushing. Then, when the producer thread
terminates, I flush the write buffer, break the loop when the read
buffer gets an EOF, schedule a close on the connection that's supposed
to deliver the messages to the final destination, and run the loop
until the external connection is closed. This seems to solve the
problem for me.

On Thu, Feb 22, 2018 at 7:07 AM, Azat Khuzhin <a3at.mail@xxxxxxxxx> wrote:
> On Tue, Feb 20, 2018 at 02:03:41PM +0300, Egor Tensin wrote:
>> Hello,
>>
>> Hopefully you can help me with a libevent issue that I'm stuck on. I
>> have two threads, one of which produces data, and the other runs
>> event_base_dispatch (receiving the data from the first thread,
>> processing it and sending it somewhere else). They communicate using a
>> pair of bufferevents (over a local socket pair). The issue is, the
>> first thread might be done with producing the data, program
>> termination being the next logical step. But I want to wait for the
>> second thread to 1) receive _all_ the data the first thread sent, 2)
>> process it and 3) send it to the final destination. I can't figure out
>> how to accomplish that, because if I call event_base_loopexit after
>> the first thread stops, it looks like the second thread exits right
>> after executing the current round of callbacks, discarding the latest
>> data that might have been sent by the first thread. Can somebody
>> please explain to me how to fix this? I can set a random timeout for
>> event_base_loopexit, but this is unreliable, isn't it?
>
> Can you share an example?
>
> Do you have two event_base, i.e. one loop per thread?
>
> In this case if you stops the producer's loop you have to do this only
> once it send all data, and if you use event_base_loopexit() (and not
> event_base_loopbreak()) then it is likely that all data will be sent,
> since the loopexit handle all queued events before exit.
>
> And so after, you should event_base_loopexit() of the consumer's thread,
> once it receives all data (I don't know), and basically most of the time
> it will be enough to just call event_base_loopexit() again. But not
> always, since there can be some data pending, so maybe you want to
> implement some logical exit, i.e. once you receive *complete message*.
>
> And like Aaron suggests it can be enough to call loop with EVLOOP_ONCE,
> but sometimes it will hang the program (in case you don't have data to
> process, i.e. very fast exit)
>
> Regards,
> Azat.
> ***********************************************************************
> To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
> unsubscribe libevent-users    in the body.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.