How to avoid synchronization overhead with threads writing to single value?

I have a shared variable DISPLAY_LEVEL that is created/used by my app, and is almost never written to, but is constantly read from the UI. The variable can be written to from several threads.

I do not care if reading this variable sometimes returns stale out of date values, as long as the values it does return are within the range of expected values.

Because the variable is generated by the user, I can't make it a static variable. As a first pass, I am using an Arc<AtomicU64>, however, given how constantly this value is read, I wonder if this is too much overhead. (It needs to be 64bit, not 8 bit for reasons that aren't important here). Are there any suggestions on how I can optimize this, keeping in mind that it is essentially almost readonly, and will be changed only a few times or potentially never?

Relaxed atomics are generally the same as just reading memory without synchronization. That's the best option.

Thanks, I am already doing that, good to know.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.