Chapter 18. Event queues

The x::eventqueue template implements a thread-safe queue of objects which uses an event file descriptor for signaling. Adding objects to the queue reports an event to the file descriptor. Waiting for an object to be added to the queue waits for an event on the file descriptor.

x::eventfd ev(x::eventfd::create());

typedef x::eventqueue<std::string> queue_t;

queue_t q(queue_t::create(ev));

This example creates a queue that contains std::string.

q->event("rosebud");

event() adds a new object to the queue.

if (!q->empty())
{
     doSomething(q->pop());
}

empty() indicates whether the queue is empty. If not, pop() removes the first object from the queue. If the associated event file descriptor is a blocking file descriptor, calling empty() is optional. If the queue is empty, pop() waits for an object to be added to the queue (presumably by another thread), and returns it. empty() is required when the associated event file descriptor is non-blocking.