Код: Выделить всё
template
struct Stack {
struct Node {
T t;
shared_ptr next;
};
atomic head;
void push_front(T t) {
auto p = make_shared(std ::move(t), head.load());
while (!head.compare_exchange_weak(p->next, p)) {}
}
optional pop_front() {
auto p = head.load();
while (p != nullptr && !head.compare_exchange_weak(p, p->next)) {}
if (p != nullptr)
return {std ::move(p->t)};
else
return {};
}
};
Код: Выделить всё
template
struct Stack {
struct Node {
T t;
Node* next;
};
std::atomic head;
void push_front(T t) {
auto p = new Node(std::move(t), head.load());
while (!head.compare_exchange_weak(p->next, p)) {}
}
std::optional pop_front() {
auto p = head.load();
while (p != nullptr && !head.compare_exchange_weak(p, p->next)) {}
if (p != nullptr)
return { std::move(p->t) };
else
return {};
}
};
Подробнее здесь: https://stackoverflow.com/questions/793 ... ed-pointer