Skip to content

Commit ff98c30

Browse files
committed
[util.smartptr.atomic] Add example from Matthew Butler.
1 parent 19e6a59 commit ff98c30

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

source/atomics.tex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,35 @@
27362736
resulting from this is performed.
27372737
\end{note}
27382738

2739+
\pnum
2740+
\begin{example}
2741+
\begin{codeblock}
2742+
template<typename T> class atomic_list {
2743+
struct node {
2744+
T t;
2745+
shared_ptr<node> next;
2746+
};
2747+
atomic<shared_ptr<node>> head;
2748+
2749+
public:
2750+
auto find(T t) const {
2751+
auto p = head.load();
2752+
while (p && p->t != t)
2753+
p = p->next;
2754+
2755+
return shared_ptr<node>(move(p));
2756+
}
2757+
2758+
void push_front(T t) {
2759+
auto p = make_shared<node>();
2760+
p->t = t;
2761+
p->next = head;
2762+
while (!head.compare_exchange_weak(p->next, p)) {}
2763+
}
2764+
};
2765+
\end{codeblock}
2766+
\end{example}
2767+
27392768
\rSec3[util.smartptr.atomic.shared]{Partial specialization for \tcode{shared_ptr}}
27402769
\indexlibraryglobal{atomic<shared_ptr<T>>}%
27412770
\begin{codeblock}

0 commit comments

Comments
 (0)