I read the following article by Antony Williams and as I understood in addition to the atomic shared count in std::shared_ptr
in std::experimental::atomic_shared_ptr
the actual pointer to the shared object is also atomic?
But when I read about reference counted version of lock_free_stack
described in Antony's book about C++ Concurrency it seems for me that the same aplies also for std::shared_ptr
, because functions like std::atomic_load
, std::atomic_compare_exchnage_weak
are applied to the instances of std::shared_ptr
.
template <class T>
class lock_free_stack
{
public:
void push(const T& data)
{
const std::shared_ptr<node> new_node = std::make_shared<node>(data);
new_node->next = std::atomic_load(&head_);
while (!std::atomic_compare_exchange_weak(&head_, &new_node->next, new_node));
}
std::shared_ptr<T> pop()
{
std::shared_ptr<node> old_head = std::atomic_load(&head_);
while(old_head &&
!std::atomic_compare_exchange_weak(&head_, &old_head, old_head->next));
return old_head ? old_head->data : std::shared_ptr<T>();
}
private:
struct node
{
std::shared_ptr<T> data;
std::shared_ptr<node> next;
node(const T& data_) : data(std::make_shared<T>(data_)) {}
};
private:
std::shared_ptr<node> head_;
};
What is the exact difference between this two types of smart pointers, and if pointer in std::shared_ptr
instance is not atomic, why it is possible the above lock free stack implementation?