On 2016-03-13 18:29,
thibault...@telecom-paristech.org wrote:
> Shared memory segments are zero initialized by OS upon creation.
> That is guaranteed by allocate_or_open_shm(), if you like.
>
> Well, at least on Windows it is not true - you have to zero the memory
> yourself.
You don't:
The initial contents of the pages in a file mapping object backed by
the operating system paging file are 0 (zero).
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366537%28v=vs.85%29.aspx
And I'm sure any operating system gives similar guarantees. Otherwise
this would be a huge security hole.
> What's more, the clear state for an atomic_flag is not always
> 0, on some platforms a cleared atomic_flag.
Formally, that is true. But I'm yet to see the architecture where the
cleared atomic_flag has non-zero representation.
> Which means your function
> still has to initialize the flag - why force it to be explicit, while it
> fit perfectly the semantics of the default constructor ?
On the architecture with non-zero cleared atomic_flag the atomic_flag
would be unusable in the context I described, unless the OS luckily
initializes the shared memory with the same value as the cleared
atomic_flag. Having non-trivial constructor won't help in this case.
> The idea is not to remove ATOMIC_FLAG_INIT. It is that you can omit it
> because the default constructor will act the same. If you have a case
> where no constructor will be called, then you have to initialize it
> explicitely - but for most cases the constructor can be called.
ATOMIC_FLAG_INIT does not add any value if the default constructor
initializes the flag already. There's no point in having both.
I'm not sure what particular use cases were considered when the
committee standardized ATOMIC_FLAG_INIT without saying that it's
actually a fancy way of zero initialization. I mean, I understand that
not requiring that the cleared state is zero potentially makes the
language more portable, but in reality this just limits the usefullness
of atomic_flag, if you are a language purist. Perhaps there are other
cases when the trivial default constructor is essential and
ATOMIC_FLAG_INIT representation is not that I'm missing.
> The
> following spinlock, for example, won't work correctly, because it lacks
> ATOMIC_FLAG_INIT. Adding it is not intuitive, as it is the only place of
> the standard library where an explicit initialization is required:
By far, it is not the only place.
I don't think I see the problem. The one who writes the spinlock class
will just write it the right way. If he doesn't, he probably shouldn't
be writing it in the first place and use an existing implementation
(gladly, there are plenty of them).
> My point is that the triviality of the default constructor of
> std::atomic and std::atomic_flag is an important feature and it has
> to stay that way.
>
> The point of your example is not to call or not a constructor. It is
> that technically you're using an object before it is constructed
> (shm_header), and you have to insure that when it is constructed it does
> not default-initialize the flag to "uninitialized" to avoid multiple
> initialization. Which, in the case of atomic_flag, can get solved by
> adding a constructor for a specific value.
Again, if atomic_flag constructor is not trivial (e.g. because it
initialized the storage), then atomic_flag is not usable in the context
I presented. The change you're proposing solves the problem of missing
initialization (a developer's error) while it prohibits the use of
atomic_flag in shared memory to control initialization. IMHO, that
exchange is not worthwhile.