Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #123839

Re: std::atomic work as volatile, or not, I think not...

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: std::atomic work as volatile, or not, I think not...
Date 2026-05-11 22:19 +0200
Organization A noiseless patient Spider
Message-ID <10ttdjo$1daks$1@dont-email.me> (permalink)
References (2 earlier) <10ts3vt$3v1g3$1@dont-email.me> <10ts5ur$updt$2@dont-email.me> <10tscpt$3vdv3$1@dont-email.me> <10tse1h$11bk8$1@dont-email.me> <10tsrcj$4pi$1@dont-email.me>

Show all headers | View raw


On 11/05/2026 17:08, boltar@caprica.universe wrote:
> On Mon, 11 May 2026 13:20:17 +0200
> David Brown <david.brown@hesbynett.no> gabbled:
>> On 11/05/2026 12:59, boltar@caprica.universe wrote:
>>> On Mon, 11 May 2026 11:02:19 +0200
>>> The underlying implementation will be the same.
>>
>> <https://cppreference.com/cpp/thread#Semaphores>
>> <https://cppreference.com/cpp/atomic/atomic>
>>
>> I don't know how you can see them as being similar, unless by "the 
>> underlying mechanism" you mean memory accesses along with processor- 
>> specific features like bus lock prefixes, load-locked/store- 
>> conditional sequences, CAS operations, memory barrier instructions, etc.
> 
> Nice cut and paste. Yes, I'm talking about the CPU level.
> 
>> In particular, semaphores have blocking support - you can block until 
>> a semaphore is available.  Atomics do not block, and are critical to 
>> non-blocking algorithms.
> 
> And? Semaphores don't have to block.

Of course you don't /have/ to block on a semaphore, but there's not much 
use of them if you don't.  You might as well just use an atomic counter, 
which is much cheaper.

You can't use an atomic object for blocking.  C++ has a nice interface 
that appears to do so, but it's actually using a futex or similar 
mechanism for the blocking.  Remember, an atomic int takes 4 bytes of 
space (on most platforms), just like an int, and a relaxed read or write 
is just a normal load or store - something that can block needs queues 
of blocked threads and OS calls.  They are different worlds.

> 
>>>> critical sections or other more expensive operations.  Read up about 
>>>> non-blocking or lock-free algorithms.
>>>
>>> An atomic/semaphore is just a lock on a single value by another name.
>>
>> Neither of them are locks.
> 
> They're locks in the sense that nothing else can touch the value while the
> current thread is updating it. So if another thread tries it'll be blocked.
> You can call it what you like but locking seems a good enough word to me.
> 

That is not what anyone else means by locks (in this context).  A lock 
is something you use to control access to something else - not just to 
the single object.

>>> Also IIRC the "+=" operation isn't atomic with std::atomic which is a 
>>> bit
>>> of a joke.
>>>
>>
>> The operator "+=" on std::atomic integer and pointer types is defined 
>> as "fetch_add(arg) + arg" - the operation on the atomic variable is 
>> atomic.
> 
> Its not atomic at the code level so its effectively useless.
> 

That makes no sense at all.  "Atomic" does not mean "uses one assembly 
instruction".

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-10 21:51 -0700
  Re: std::atomic work as volatile, or not, I think not... David Brown <david.brown@hesbynett.no> - 2026-05-11 08:25 +0200
    Re: std::atomic work as volatile, or not, I think not... boltar@caprica.universe - 2026-05-11 08:28 +0000
      Re: std::atomic work as volatile, or not, I think not... David Brown <david.brown@hesbynett.no> - 2026-05-11 11:02 +0200
        Re: std::atomic work as volatile, or not, I think not... boltar@caprica.universe - 2026-05-11 10:59 +0000
          Re: std::atomic work as volatile, or not, I think not... David Brown <david.brown@hesbynett.no> - 2026-05-11 13:20 +0200
            Re: std::atomic work as volatile, or not, I think not... Bonita Montero <Bonita.Montero@gmail.com> - 2026-05-11 13:22 +0200
              Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-11 13:00 -0700
            Re: std::atomic work as volatile, or not, I think not... boltar@caprica.universe - 2026-05-11 15:08 +0000
              Re: std::atomic work as volatile, or not, I think not... David Brown <david.brown@hesbynett.no> - 2026-05-11 22:19 +0200
                Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-11 13:25 -0700
            Re: std::atomic work as volatile, or not, I think not... scott@slp53.sl.home (Scott Lurndal) - 2026-05-11 15:48 +0000
              Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-11 13:09 -0700
          Re: std::atomic work as volatile, or not, I think not... Bonita Montero <Bonita.Montero@gmail.com> - 2026-05-11 13:21 +0200
          Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-11 12:48 -0700
            Re: std::atomic work as volatile, or not, I think not... red floyd <no.spam.here@its.invalid> - 2026-05-11 21:04 -0700
              Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-14 00:35 -0700
        Re: std::atomic work as volatile, or not, I think not... cross@spitfire.i.gajendra.net (Dan Cross) - 2026-05-11 11:53 +0000
          Re: std::atomic work as volatile, or not, I think not... David Brown <david.brown@hesbynett.no> - 2026-05-11 15:18 +0200
            Re: std::atomic work as volatile, or not, I think not... Bonita Montero <Bonita.Montero@gmail.com> - 2026-05-11 15:20 +0200
              Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-11 12:54 -0700
  Re: std::atomic work as volatile, or not, I think not... "Fred. Zwarts" <F.Zwarts@HetNet.nl> - 2026-05-11 11:46 +0200
    Re: std::atomic work as volatile, or not, I think not... David Brown <david.brown@hesbynett.no> - 2026-05-11 12:40 +0200
      Re: std::atomic work as volatile, or not, I think not... Chris Ahlstrom <OFeem1987@teleworm.us> - 2026-05-11 06:55 -0400
        Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-12 12:14 -0700
    Re: std::atomic work as volatile, or not, I think not... scott@slp53.sl.home (Scott Lurndal) - 2026-05-11 15:49 +0000
      Re: std::atomic work as volatile, or not, I think not... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-05-11 12:36 -0700

csiph-web