简体   繁体   中英

A puzzle about the push() in yqueue.hpp of zeromq v 3.2.3

Recently, I am trying to read the source code of zeromq with version 3.2.3. Now I have a puzzle about the push() function in yqueue.hpp ?

the source code is :

//  Adds an element to the back end of the queue.
    inline void push ()
    {
        back_chunk = end_chunk;
        back_pos = end_pos;

        if (++end_pos != N)
            return;

        chunk_t *sc = spare_chunk.xchg (NULL);
        if (sc) {
            end_chunk->next = sc;
            sc->prev = end_chunk;
        } else {
            end_chunk->next = (chunk_t*) malloc (sizeof (chunk_t));
            alloc_assert (end_chunk->next);
            end_chunk->next->prev = end_chunk;
        }
        end_chunk = end_chunk->next;
        end_pos = 0;
    }

keeping to move the position of "end_pos", if it doesn't equal N, "return". I am confused about this. can some explain it for me ? thank you

Variable N is part of the class template as defined here:

template <typename T, int N> class yqueue_t

If you take a look at the comments at the top of the class you get exactly what N means:

//  yqueue is an efficient queue implementation. The main goal is
//  to minimise number of allocations/deallocations needed. Thus yqueue
//  allocates/deallocates elements in batches of N.
//
//  yqueue allows one thread to use push/back function and another one 
//  to use pop/front functions. However, user must ensure that there's no
//  pop on the empty queue and that both threads don't access the same
//  element in unsynchronised manner.
//
//  T is the type of the object in the queue.
//  N is granularity of the queue (how many pushes have to be done till
//  actual memory allocation is required).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM