Boost logo

Boost :

Subject: Re: [boost] boost.lockfree update
From: David Abrahams (dave_at_[hidden])
Date: 2010-07-24 17:16:46


At Sat, 24 Jul 2010 22:07:44 +0200,
Tim Blechmann wrote:
>
> >>> 3. For the empty() method, it says "Not thread-safe, use for debugging
> >>> purposes only". Does this mean calling it might destroy the data
> >>> structure's invariants? Or is it always safe in that regard? In which
> >>> scenarios can it be used, specifically?
> >>>
> >> calling it may return a wrong result. the data structure is not
> >> corrupted.
> >>
> > Am I right in assuming that the result it produces is correct when it is
> > produced, and the only problem is that something could get added to the
> > queue before the caller gets a chance to examine the result?
>
> for empty(), the following steps need to be done:
> 1. atomically load the head pointer
> 2. atomically load the tail pointer
> 3. compare the pointers.
>
> if the state of the fifo is changed between steps 1 and 2, the result
> doesn't reflect the state of the fifo anymore ...

Not that that matters. The state of the FIFO could be changed after
step 3 and before you use the result of the empty() call. In general,
predicates about a data structure that might be modified from another
thread are useless unless you can afford for them to give you the
wrong answer.

Sometimes, the nature of the data structure and how it's used can make
such predicates a little less-useless. For example, if thread A is
the only one that removes elements from a FIFO, a true result from
empty() tells thread A nothing useful, but a false result actually
means the FIFO is non-empty in a useful way, because it will remain so
no matter what happens on the other end of the FIFO, until A removes
at least one element.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk