atomic variables?

142 views
Skip to first unread message

Roger

unread,
Nov 7, 2024, 5:55:01 PMNov 7
to golang-nuts
Hi,

True or false, it's better to avoid loading atomic variables unnecessarily? 

Ian Lance Taylor

unread,
Nov 7, 2024, 6:13:38 PMNov 7
to Roger, golang-nuts
On Thu, Nov 7, 2024 at 2:54 PM Roger <rogerdilm...@gmail.com> wrote:
>
> True or false, it's better to avoid loading atomic variables unnecessarily?

Questions like this always depend on the context. There is no answer
that is always correct in all situations.

That said, all else being equal, it's better to avoid loading any
variable unnecessarily. That applies to both ordinary variables and
atomic variables.

Ian

scott beeker

unread,
Nov 7, 2024, 7:25:58 PMNov 7
to golang-nuts
True
However, it's important to note that in some cases, multiple loads might be necessary for correctness, such as in certain lock-free algorithms or when implementing specific memory ordering requirements
citation
.
When working with atomic variables, it's best to:
  • Only use atomic operations when necessary for thread-safety and synchronization.
  • Choose the appropriate memory ordering for each operation to avoid overly strict guarantees when not needed.
  • Consider using higher-level synchronization primitives (like mutexes or channels) for more complex scenarios, as they can be easier to reason about and may have better performance characteristics in some cases.
Remember, the key is to balance correctness with performance, using atomic operations judiciously and only when required for proper synchronization between threads.

Roger

unread,
Nov 8, 2024, 7:31:50 AMNov 8
to scott beeker, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/3d9a1a9f-bb76-4abb-9ac0-8c24bf8d78c3n%40googlegroups.com.

Eli Lindsey

unread,
Nov 8, 2024, 8:34:38 AMNov 8
to Roger, golang-nuts
Relaxed atomics can be significantly easier to misuse than they first appear. If this is something you’ll be working with, I’d highly recommend watching Herb Sutter’s ~3hr workshop on atomics - it will cover many of the things you seem interested in (overhead of atomics vs regular loads/stores on various hardware platforms, relaxed atomics, problems that arise from being too clever, and so on)

Reply all
Reply to author
Forward
0 new messages