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

RE: [Libevent-users] "Triggering" writes in an event loop from separate threads



Hi Jan,

As I understand:
1. You want to notify your event loop that you want to do write operations on a bufferevent.
2. Other threads add frames to a linked list.
3. These threads should some how notify to the thread of the event loop that it should write these frames using bufferevent_write.

I suggest the following solution:

Each time a thread wants to tell the event loop thread that data needs to be written you can use the event_base_once command:

event_base_once(*your event loop, -1, callback_func, EV_TIMEOUT, *ctx containing your bufferevent and linkedlist, NULL);

This will switch an event to your event loop thread.
In the callback_func use bufferevent_write.

It might also be done with paired bufferevents, but I'm not sure paired bufferevents switch to the thread of the event_loop. (I would expect it to do so... But from what I remember it did not work for me).

Good luck,
Tomer.
________________________________________
From: owner-libevent-users@xxxxxxxxxxxxx <owner-libevent-users@xxxxxxxxxxxxx> on behalf of Jan Danielsson <jan.m.danielsson@xxxxxxxxx>
Sent: Friday, August 09, 2013 2:15 AM
To: libevent-users@xxxxxxxxxxxxx
Subject: [Libevent-users] "Triggering" writes in an event loop from separate threads

Hello,

   I have an eventloop with a bufferevent set up in an application. When
handling read events everything is done on the event loop thread, and
some writes are done as a direct result of data being read there, but
writes are typically placed on a queue by a separate threads. But I want
the writes to be handled on the eventloop thread as well.

   i.e. I have a readcb() function which reads incoming frames from the
remote system. Each frame typically leads to some kind of response.
These I can send directly using bufferevent_write() as soon as the
complete frames have been received and parsed. However, the bufferevent
also has a context associated with it which contains a mutex and a
linked list which is shared with the rest of the application. Other
parts of the application can place frames on the outqueue at any time.

   Could one set up both a read and write callback, but not enable
EV_WRITE. Then, on the separate threads, lock the outqueue, add frames
to the queue, unlock the queue and finally call bufferevent_enable(bev,
EV_WRITE)? I.e. would enabling EV_WRITE cause the write callback to be
called? ..and once all the frames have been written, the write callback
would call bufferevent_disable(bev, EV_WRITE) again?

   .. essentially using the EV_WRITE flag as a condvar, in some
perverted sense. Does switching on EV_WRITE trigger a call to the write
callback? Is it safe to do it from another thread? (Yes, I have enabled
thread support in the library, and am initializing the thread support
properly).

   Is there some other way to do it? (For various reasons I don't want
to call bufferevent_write() directly from the threads which generate
output frames).

   /Jan
***********************************************************************
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.