Boost logo

Boost :

Subject: Re: [boost] [lockfree] how many items are queued?
From: Tim Blechmann (tim_at_[hidden])
Date: 2009-09-03 05:14:11

hi oliver,

> lockfree.fifo provides the info if the queue is empty or not.

it is mainly done as debugging help, but it is not supposed to be used
in concurrent code ...

> is it possible to provide a function std::size_t size() const - returning how many items are queued at a specific timepoint?

there is no real way to implement it reliably, so i don't want to add
this to the interface.
if it would be implemented by iterating the internal linked list,
objects could be inserted during the iteration (making the count
incorrect). using an atomic integer, would also not be precise, since
incrementing the integer and manipulating the fifo cannot be done
atomically ...

if you need to know, how many items are approximately in the queue, i'd
suggest you use an atomic counter in your code, something like (pseudocode):

fifo f;
atomic_int c;

bool enqueued = f.enqueue();
if (!enqueued) --c;


bool dequeued = f.dequeue();
if (dequeued) --c;

in this case, c is not the exact value of queue items, but an upper
bound ...

i don't really want to add more functions to the api, that are not
thread-safe ... would this workaround fit your needs?

cheers, tim

Relying on the government to protect your privacy is like asking a
peeping tom to install your window blinds.
  John Perry Barlow

Boost list run by bdawes at, gregod at, cpdaniel at, john at