[zeromq-dev] Reimplementation of the zring class with equipotent API

Goswin von Brederlow goswin-v-b at web.de
Sat Sep 27 07:08:32 CEST 2014


I just send a pull request for a reimplementation of the zring class
with a new API:


Note: I renamed the existing zring class to zhashring in case that is
still wanted by others.

The ztimeout class, for which I write the initial zring class, needs a
method do directly remove/move elements of the ring. With the inital
API this exposes internas of the implementation and needs a dangerous
zring_goto mothod. So it was time to try something different.

The new zring API uses the zring_t type for each element in the ring.
In fact each element is a zring_t, they are all equipotent. Given any
element you can add, remove, iterate, ... to the ring. And elements
can be moved safely from one ring to another (or within the same
ring). Instead of a special head structure every element of the ring
can function as head. That means you have to have an item in the ring
for there to be a ring. An empty ring is just a NULL pointer.

But sometimes one needs to have a head for an empty ring (to set the
destructor and duplicator even though the ring is empty). For this I
defined guard elements. A guard element is simply an element in the
ring that has NULL as item pointer. Otherwise they are like any other
element of the ring.

Except for zring_first/zring_next. Both of them skip over guard
elements and only return elements with a real item. And yes, I said
"elements". You can have more than one guard in a ring, splitting them
into sections. Zring_first and zring_next allow iterating until the
next guard, once around the ring or until any specific element is

For an example of how zring can be used look at ztimeout. This
includes use of multiple guard elements and the different iterator


More information about the zeromq-dev mailing list