<div dir="ltr">On Wed, Nov 29, 2023 at 4:09 PM Ross Finlayson <<a href="mailto:finlayson@live555.com">finlayson@live555.com</a>> wrote:<br>><br>> > On Nov 30, 2023, at 4:14 AM, Dmitry Bely <<a href="mailto:d.bely@recognize.ru">d.bely@recognize.ru</a>> wrote:<br>> ><br>> > Hi,<br>> > Recent versions of live555 use std::atomic_flag array to work with<br>> > event triggers. Consider the following code fragment:<br>> ><br>> > #ifndef NO_STD_LIB<br>> > if (fTriggersAwaitingHandling[i].test()) {<br>> > fTriggersAwaitingHandling[i].clear();<br>> > #else<br>> ><br>> > It is problematic in two ways: 1) it's not atomic: the value can be<br>> > changed elsewhere between test() and clear()<br>><br>> 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<br>> <a href="http://live555.com/liveMedia/faq.html#threads">http://live555.com/liveMedia/faq.html#threads</a><br>> 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<br>> fTriggersAwaitingHandling[i].test_and_set()<br>> 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.<br><br><div>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"?</div><br>> > and 2) it requires C++>=20.<br>><br>> 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.<br><br>Unfortunately it does - std::atomic_flag::test() appeared first in C++20, see <a href="https://en.cppreference.com/w/cpp/atomic/atomic_flag">https://en.cppreference.com/w/cpp/atomic/atomic_flag</a>. 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?)...
<br><br><div>> 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.</div><div><br></div><div>Sure. But if you can make it work out of the box for any modern C++ compiler, why not do it?<br></div><div><br></div><div>- Dmitry Bely</div><div><br></div></div>