[MAINT] Remove the pthread_once based implementation for existing singletons. #3115
Labels
[core]
Area: Changes in SRT library core
Type: Maintenance
Work required to maintain or clean up the code
Milestone
There is a standard and acceptable way for creating singletons in C++: create the object as a local static variable and return its reference. Some of global objects, notably the
CUDTUnited
object, is implemented this way.There was an alternative implementation provided, however, which was based on the call of
pthread_once
. This should be removed.The local-static-based singleton (known from the Microsoft's documentation as "Magic statics") has been guaranteed to be initialized properly at the first call of the function containing it, including thread safety (deadlock-free) during the initialization. Hence there is nothing more to do than just create a singleton in the form of a local static.
The guarantees have been introduced in C++11, however what counts here is not which standard is in use during compiling, but rather whether a compiler provides these guarantees and under which condition - and this depends on whether the compiler supports it and whether any flags need to be turned on, regardless of what standard is to be honored by the compiler. We have actually three compiler categories here:
-fno-threadsafe-statics
; default is on./Zc:threadSafeInit-
, not exactly mentioned anything about Linux/Mac versions, although the thread safety for local statics is on by default.This means then, for all compilers this project is aware of, thread-safe locals are supported by the compilers and the support is turned on by default. SRT should provide some warnings in the documentation that it makes use of particular features (so,
-fno-threadsafe-statics
option shall not be used or anyhow enforced during the build), but the thread-safe locals can be otherwise stated as supported and no alternative solution is required.The text was updated successfully, but these errors were encountered: