Hacker News new | ask | show | jobs
by grogers 958 days ago
Context matters. In the context of a single thread, ordinary variable reads and writes happen as if they were in program order. So in that context, they are extremely natural. When shared across threads, reading and writing ordinary variables happens under a lock, so everything still appears as if in program order.

Atomics are used in a context of being shared across threads without locks. In that context, reasoning about them makes you actually think about memory ordering in a way that wasn't there with ordinary variables.

Sequentially consistent atomics are the easiest to reason about because all seq_cst atomic operations within a thread happen in program order (and are visible to other threads in that order). Relaxing that to acquire/release/relaxed loosens that, so it requires more thinking about what needs to be visible and in what order. It's only complicated because seq_cst has a performance cost, so you typically want to think about whether it's necessary or not for the particular use case. But reasoning about what is happening with an algorithm when using seq_cst atomics is always at least as easy as when using looser ordering.

1 comments