Thread overview
`shared Mutex`?
Dec 28, 2014
Aiden
Dec 28, 2014
Artur Skawina
Dec 28, 2014
Meta
Dec 28, 2014
Aiden
Dec 28, 2014
ketmar
Dec 29, 2014
Aiden
Dec 29, 2014
Aiden
December 28, 2014
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?
December 28, 2014
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
December 28, 2014
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.
December 28, 2014
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!
December 28, 2014
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 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!
you can turn that method to template. for now it is virtual method and compiler is unable to inline it.


December 29, 2014
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`?
December 29, 2014
Or do you mean that I should simply make the property `final` so it can be inlined?