|
Boost : |
From: Andy Glew (glew_at_[hidden])
Date: 1999-12-30 18:33:29
> The smart pointers are just as thread-safe as integers.
No they aren't! This is what I was trying to say in my reply
to Dave A.
Ordinary scalars - integers, pointers, etc. - on most normal
hardware (I could probably say *all* hardware that anyone in this
group is programming on, but it might fail for certain 8 bit wide
bus embedded systems) have atomicity guarantees:
Specifically, you can *read* and *write* a suitably aligned
8 bit, 16 bit, 32 bit, and, probably, 64 bit, quantity, and are
guaranteed that it is done atomically.
(Intel x86 far mode pointers, with segment/offset stored
separately, are not atomic. But flat mode pointers are.)
If your system makes additional ordering guarantees, like my P6
speculative processor consistency or SUN's TSO, then you can use
this atomicity to implement multiprocessor/multithread algorithms
in ways that are much, much, cheaper than using a mutex, or even
using hardware provided atomic RMW or LL/SC.
As I tried to point out in my reply to Dave A., the smart pointers
are not atomic. Two simultaneous writes by different threads may
interfere - in the example given, causing a double delete.
Multithread algorithms and data structures have a hierarchy of
sophistication:
a) data structures that are MP safe,
which can *appear* to have simultaneous accesses of all kinds
- which either do this, or where the synchronization is hidden
behind an API.
b) data structures that are read and write atomic.
Typically, you end up replicating such data structures
at least 1 per thread, with ordering rules and simple
algorithms controlling access
c) data structures that require mutex,
because no accesses are atomic
Integers are in b).
Boost's smart pointers so far are in c)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk