logoalt Hacker News

CyberDildonicsyesterday at 10:07 PM2 repliesview on HN

Atomics have almost no impact when reading, which is what would happen in a shared pointer the vast majority of the time.


Replies

woodruffwyesterday at 10:20 PM

> which is what would happen in a shared pointer the vast majority of the time.

This seems workload dependent; I would expect a lot of workloads to be write-heavy or at least mixed, since copies imply writes to the shared_ptr's control block.

oconnor663yesterday at 11:17 PM

I think it's pretty rare to do a straight up atomic load of a refcount. (That would be the `use_count` method in C++ or the `strong_count` method in Rust.) More of the time you're doing either a fetch-add to copy the pointer or a fetch-sub to destroy your copy, both of which involve stores. Last I heard the fetch-add can use the "relaxed" atomic ordering, which should make it very cheap, but the fetch-sub needs to use the "release" ordering, which is where the cost comes in.