What is the point of a synchronized lock on a single return statement?

Robert Schadek rschadek at symmetryinvestments.com
Mon Nov 25 16:16:45 UTC 2019


But be aware, even though the bool is returned from a 
synchronized block,
its actual value has no meaning at all.

All the meaning you get out of that bool is that the MessageBox 
was closed
when you called that function.
If there is a function in MessageBox that can reopen the instance,
you can not assume that the MessageBox is still closed when you
read the bool.
Assuming your program has more than one thread touching that 
instance of
the MessageBox.

Consider

```
auto mb = new MessageBox();
bool c = mb.isClosed();
// this thread gets preempted, and another thread
// does something with mb that changes its state

if(!c) { // the value of c might not what you expected
    // start rockets
}
```

This problem, at least partly, spawn concepts like Monitors,
the Rendezvous concept, Message Passing, and others.


More information about the Digitalmars-d-learn mailing list