Synchronized classes have no public members

Artur Skawina via Digitalmars-d digitalmars-d at puremagic.com
Wed Oct 14 05:33:18 PDT 2015


On 10/14/15 03:10, Kapps via Digitalmars-d wrote:
> On Tuesday, 13 October 2015 at 18:28:23 UTC, Marco Leise wrote:
>>
>> Guys, sorry to break into your wishful thinking, but
>>
>>    synchronized(mutex) {}
>>
>> already works as you want it to since as long as I can think. Yes, it takes a parameter, yes it calls lock/unlock on the mutex. :)
> 
> Though really, that could just be:
> with(mutex.lock()) { }

Yes, but be careful - this is a relatively recent language change
(AFAIR it went in during the 'we-don't-need-no-changelog' phase,
so this was only documented in a bugzilla entry) _and_ it used to
be broken until /very/ recently - the object was destroyed before
entering the block. IOW it was executed like:

    auto __tmp = mutex.lock();
    __tmp.__dtor();
    {
       ...
    }

which could result in nasty bugs, because you might not have 
immediately noticed that you're operating on an already unlocked
object.

artur


More information about the Digitalmars-d mailing list