On Wed, Nov 29, 2023 at 4:09 PM Ross Finlayson <finlay...@live555.com>
wrote:
>
> > On Nov 30, 2023, at 4:14 AM, Dmitry Bely <d.b...@recognize.ru> wrote:
> >
> > Hi,
> > Recent versions of live555 use std::atomic_flag array to work with
> > event triggers. Consider the following code fragment:
> >
> > #ifndef NO_STD_LIB
> >      if (fTriggersAwaitingHandling[i].test()) {
> >       fTriggersAwaitingHandling[i].clear();
> > #else
> >
> > It is problematic in two ways: 1) it's not atomic: the value can be
> > changed elsewhere between test() and clear()
>
> What you’re missing here is that the event loop (which contains the code
that you quote above) is intended to be run only by a single thread.  See
>         http://live555.com/liveMedia/faq.html#threads
> The *only* LIVE555 code that is meant to ever be run in a separate thread
(i.e., other than the thread that runs the event loop) is “triggerEvent()”,
which calls
>         fTriggersAwaitingHandling[i].test_and_set()
> I.e., a non-event-loop thread can only ever set an atomic flag (using the
atomic operation “test_and_set()”); it cannot clear it.

OK, but atomic compare-and-exchange is logically clearer, even if you
always use it from a single thread. BTW I'm curious what the purpose of
std::atomic_flag? What was wrong with good old "volatile bool"?

> > and 2) it requires C++>=20.
>
> No it doesn’t ‘require’ C++>=20.  The code should be supported on any
compiler that supports "std::atomic_flag”.  That seems to include all
recent versions of clang, BTW.

Unfortunately it does - std::atomic_flag::test() appeared first in C++20,
see https://en.cppreference.com/w/cpp/atomic/atomic_flag. E.g. GCC 8, that
I currently use, declares full C++17 support and doesn't have that method.
At the same time the rest of your codebase just needs C++98(03?)...

> But if you don’t have "std::atomic_flag”, you can compile the code with
-DNO_STD_LIB, and it should still work OK if you are using multiple threads
as intended - i.e. running all LIVE555 code - except for “triggerEvent()” -
in a single thread.

Sure. But if you can make it work out of the box for any modern C++
compiler, why not do it?

- Dmitry Bely
_______________________________________________
live-devel mailing list
live-devel@lists.live555.com
http://lists.live555.com/mailman/listinfo/live-devel

Reply via email to