C++ Logo

std-discussion

Advanced search

Re: Synchronization of atomic notify and wait

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 19 Jul 2022 00:48:30 +0200
wt., 19 lip 2022 o 00:30 Andrey Semashev via Std-Discussion
<std-discussion_at_[hidden]> napisaƂ(a):
>
> On 7/19/22 00:11, Marcin Jaczewski wrote:
> > pon., 18 lip 2022 o 22:27 Andrey Semashev via Std-Discussion
> >>
> >> Obviously, wait can be implemented as there are real world
> >> implementations. The standard isn't written to demand something that is
> >> impossible to implement.
> >
> > Yes, it is possible to implement but not in pure C++ as it could use
> > compiler intrinsics
> > or assembler. It could use code that have total ordering garatees or
> > use memory feces.
> >
> > I grab random code with "wait" from:
> > https://github.com/llvm/llvm-project/blob/main/libcxx/include/atomic
> > ```
> > template <class _Atp, class _Fn>
> > struct __libcpp_atomic_wait_backoff_impl {
> > _Atp* __a;
> > _Fn __test_fn;
> > _LIBCPP_AVAILABILITY_SYNC
> > _LIBCPP_INLINE_VISIBILITY bool operator()(chrono::nanoseconds __elapsed) const
> > {
> > if(__elapsed > chrono::microseconds(64))
> > {
> > auto const __monitor = __libcpp_atomic_monitor(__a);
> > if(__test_fn())
> > return true;
> > __libcpp_atomic_wait(__a, __monitor);
> > }
> > else if(__elapsed > chrono::microseconds(4))
> > __libcpp_thread_yield();
> > else
> > {} // poll
> > return false;
> > }
> > };
> > ```
> > This could be your `wait` implmetation and it will work only in Clang.
> > I dubit it any way conform C++ in implmetation, but as whole confom
> > behavior of `wait` defined in standard text.
> >
> >
> > Another place where we have sitation like this is `std::vector` you
> > can't implmemt it in C++.
> > You can copy paste it implmetation and it will work but if you change
> > compiler it will break.
>
> There are parts of the standard that are not implementable in pure C++,
> but atomic wait in particular isn't one of them. Sure, it relies on
> atomics being implemented in hardware as required and the underlying OS
> for blocking and wakeup, but beyond that there's nothing magical about
> it. In a sense, it is no more magical than std::time().

Ok.
We are probably in violent agreement as I miss interpreted your "How
is this possible, under the current ordering rules?"
with the opposite meaning (aka that standard does not give this garatees).
I simply argue that "point #4" was enough to make `wait` work and you
say that you can easy build waint using
basic building blocks provided by standard.


> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2022-07-18 22:48:42