What is the point of a synchronized lock on a single return statement?
angel
andrey.gelman at gmail.com
Mon Nov 25 11:33:24 UTC 2019
On Monday, 25 November 2019 at 08:22:17 UTC, Andrej Mitrovic
wrote:
> From:
> https://github.com/dlang/phobos/blob/10b9174ddcadac52f6a1ea532deab3310d3a8c03/std/concurrency.d#L1913-L1916:
>
> -----
> ///
> final @property bool isClosed() @safe @nogc pure
> {
> synchronized (m_lock)
> {
> return m_closed;
> }
> }
> -----
>
> I don't understand the purpose of this lock. The lock will be
> released as soon as the function returns, and it returns a copy
> of a boolean anyway. Am I missing something here?
I think this code can be rewritten as
---
final @property bool isClosed() @safe @nogc pure
{
bool ret;
synchronized (m_lock)
{
ret = m_closed;
}
return ret;
}
---
Normally, if the memory location of m_closed is aligned, the
assignment to 'ret' should be atomic, however if you cannot make
assumptions about alignment, the access should be protected.
More information about the Digitalmars-d-learn
mailing list