33
Concurrency support library
[thread]
33.5
Atomic operations
[atomics]
33.5.8
Class template
atomic
[atomics.types.generic]
33.5.8.7
Partial specializations for smart pointers
[util.smartptr.atomic]
33.5.8.7.1
General
[util.smartptr.atomic.general]
1
#
The library provides partial specializations of the
atomic
template for shared-ownership smart pointers (
[util.
sharedptr]
)
.
[
Note
1
:
The partial specializations are declared in header
<memory>
.
—
end note
]
The behavior of all operations is as specified in
[atomics.
types.
generic]
, unless specified otherwise
.
The template parameter
T
of these partial specializations may be an incomplete type
.
2
#
All changes to an atomic smart pointer in
[util.
smartptr.
atomic]
, and all associated
use_
count
increments, are guaranteed to be performed atomically
.
Associated
use_
count
decrements are sequenced after the atomic operation, but are not required to be part of it
.
Any associated deletion and deallocation are sequenced after the atomic update step and are not part of the atomic operation
.
[
Note
2
:
If the atomic operation uses locks, locks acquired by the implementation will be held when any
use_
count
adjustments are performed, and will not be held when any destruction or deallocation resulting from this is performed
.
—
end note
]
3
#
[
Example
1
:
template
<
typename
T
>
class
atomic_list
{
struct
node
{
T t; shared_ptr
<
node
>
next;
}
; atomic
<
shared_ptr
<
node
>
>
head;
public
:
shared_ptr
<
node
>
find
(
T t
)
const
{
auto
p
=
head
.
load
(
)
;
while
(
p
&
&
p
-
>
t
!
=
t
)
p
=
p
-
>
next;
return
p;
}
void
push_front
(
T t
)
{
auto
p
=
make_shared
<
node
>
(
)
; p
-
>
t
=
t; p
-
>
next
=
head;
while
(
!
head
.
compare_exchange_weak
(
p
-
>
next, p
)
)
{
}
}
}
;
—
end example
]