C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::atomic::wait_and_write_back/wait_and_exchange

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 04 Jul 2024 10:58:58 +0200
On Wednesday 3 July 2024 23:24:09 CEST 李 秋逸 via Std-Proposals wrote:
> Here is the impl in MSVC STL:
>
> //The only data member of std::binary_semaphore is atomic<unsigned char>
> _Counter void acquire() noexcept /* strengthened */ {
> for (;;) {
> unsigned char _Prev = _Counter.exchange(0);
> if (_Prev == 1) {
> break;
> }
> _Counter.wait(0, memory_order_relaxed);
> }
> }
>
> It will change to
>
> void acquire() noexcept
> {
> _Counter.wait_and_write_back(0);
> }
>
> Observely, there is less atomic calling and if​ in the function body of
> acquire.

Why do you say there's less atomic code? You've moved the implementation of
one function into another, but have not shown this produces better or safer
code.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-07-04 08:59:01