www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - `shared Mutex`?

reply "Aiden" <dismaldenizen gmail.com> writes:
Hello all,

This is my first post on these forums. I've been learning D for 
the past couple of months or so and have been quite impressed by 
the language thus far. One stumbling block that I have 
encountered is with using `shared`, and more specifically using 
`shared` with synchronization tools like Mutex and Condition.

Consider the following program:
https://gist.github.com/anibali/4d544c31ac762409d4ea

I can't seem to get the thing working without a bunch of casts to 
and from `shared`, which I'm assuming is not a good practice - it 
definitely doesn't make for nice-looking code.

I've found an old thread on a similar 
issue(http://forum.dlang.org/thread/moyyibrpnnmrrovylkui forum.dlang.org) 
but there doesn't seem to be a conclusion there either.

Is `shared` in a workable state? Shouldn't Mutex, Condition, etc 
be shared since they are basically only ever useful when used in 
multiple threads? Am I missing the point completely?
Dec 28 2014
next sibling parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On 12/28/14 10:24, Aiden via Digitalmars-d-learn wrote:
 Is `shared` in a workable state?
No.
 Shouldn't Mutex, Condition, etc be shared since they are basically only ever
useful when used in multiple threads?
Yes, but there are so many problems with 'shared' that using it that way (even only as a type constructor) is impractical. artur
Dec 28 2014
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 28 December 2014 at 09:24:31 UTC, Aiden wrote:
 Hello all,

 This is my first post on these forums. I've been learning D for 
 the past couple of months or so and have been quite impressed 
 by the language thus far. One stumbling block that I have 
 encountered is with using `shared`, and more specifically using 
 `shared` with synchronization tools like Mutex and Condition.

 Consider the following program:
 https://gist.github.com/anibali/4d544c31ac762409d4ea

 I can't seem to get the thing working without a bunch of casts 
 to and from `shared`, which I'm assuming is not a good practice 
 - it definitely doesn't make for nice-looking code.

 I've found an old thread on a similar 
 issue(http://forum.dlang.org/thread/moyyibrpnnmrrovylkui forum.dlang.org) 
 but there doesn't seem to be a conclusion there either.

 Is `shared` in a workable state? Shouldn't Mutex, Condition, 
 etc be shared since they are basically only ever useful when 
 used in multiple threads? Am I missing the point completely?
Unfortunately, the current way to use shared is pretty much what you are doing now. It's probably one of the least understood and difficult to work with parts of the language.
Dec 28 2014
parent reply "Aiden" <dismaldenizen gmail.com> writes:
Thanks for the information. At least I've discovered a reasonably 
tidy way of wrapping Mutex up so that it's not quite as painful 
casting everything:

shared class SharedMutex {
   private Mutex mutex;

   private  property Mutex unsharedMutex() {
     return cast(Mutex)mutex;
   }

   this() {
     mutex = cast(shared)new Mutex();
   }

   alias unsharedMutex this;
}

SharedMutex can just be used like a normal Mutex, which is pretty 
neat. `alias this` is awesome!
Dec 28 2014
parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 28 Dec 2014 20:21:45 +0000
Aiden via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:

 Thanks for the information. At least I've discovered a reasonably=20
 tidy way of wrapping Mutex up so that it's not quite as painful=20
 casting everything:
=20
 shared class SharedMutex {
    private Mutex mutex;
=20
    private  property Mutex unsharedMutex() {
      return cast(Mutex)mutex;
    }
=20
    this() {
      mutex =3D cast(shared)new Mutex();
    }
=20
    alias unsharedMutex this;
 }
=20
 SharedMutex can just be used like a normal Mutex, which is pretty=20
 neat. `alias this` is awesome!
you can turn that method to template. for now it is virtual method and compiler is unable to inline it.
Dec 28 2014
next sibling parent "Aiden" <dismaldenizen gmail.com> writes:
On Sunday, 28 December 2014 at 20:36:07 UTC, ketmar via 
Digitalmars-d-learn wrote:
 you can turn that method to template. for now it is virtual 
 method and
 compiler is unable to inline it.
Are you suggesting something like the following... void lock(alias m)() if(is(typeof(m) == shared(Mutex))) { (cast(Mutex)m).lock(); } void unlock(alias m)() if(is(typeof(m) == shared(Mutex))) { (cast(Mutex)m).unlock(); } ...and then calling lock!mySharedMutex() and unlock!mySharedMutex()? Is there a way to generate a bunch of these in a similar way to `alias this`?
Dec 29 2014
prev sibling parent "Aiden" <dismaldenizen gmail.com> writes:
Or do you mean that I should simply make the property `final` so 
it can be inlined?
Dec 29 2014