www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How synchronized actually works?

reply Hipreme <msnmancini hotmail.com> writes:
I was almost going to implement a thing which I would call 
`Locked`, by using that code:

```d
struct Locked
{
     private Mutex mtx;
     this(Mutex mtx)
     {
         this.mtx = mtx;
         mtx.lock();
     }
     ~this()
     {
         mtx.unlock();
     }
}

with(Locked(myMutex))
{
}
```

But then I remembered D has a keyword called `synchronized`. Then 
I looked at the spec, and I was overjoyed by the example present 
on it:


 Example
```d synchronized { ... } ``` I really loved this example and I think it should be reproduced in a big screen everywhere in the world (complete sarcasm). I'm wanting someone more experienced in the subject to both explain in this thread and update the spec: (https://dlang.org/spec/statement.html#synchronized-statement) Also, there has been a talk on Discord for it being a Range-like interface, but for `lock` and `unlock`, instead of using only classes object but I don't understand enough about this subject.
Dec 01 2023
parent reply Nick Treleaven <nick geany.org> writes:
On Friday, 1 December 2023 at 21:01:32 UTC, Hipreme wrote:
 I'm wanting someone more experienced in the subject to both 
 explain in this thread and update the spec: 
 (https://dlang.org/spec/statement.html#synchronized-statement)
I've made a PR to improve the docs: https://github.com/dlang/dlang.org/pull/3739
Dec 04 2023
parent Hipreme <msnmancini hotmail.com> writes:
On Monday, 4 December 2023 at 13:05:38 UTC, Nick Treleaven wrote:
 On Friday, 1 December 2023 at 21:01:32 UTC, Hipreme wrote:
 I'm wanting someone more experienced in the subject to both 
 explain in this thread and update the spec: 
 (https://dlang.org/spec/statement.html#synchronized-statement)
I've made a PR to improve the docs: https://github.com/dlang/dlang.org/pull/3739
Almost missed it out but wanted to let you know, thanks a bunch for the work! It is a lot better now and I can clearly understand what it is supposed to do :)
Dec 05 2023