On 16/10/16 19:50, tcak wrote: > On Sunday, 16 October 2016 at 08:41:26 UTC, Christian Köstlin wrote: >> Hi, >> >> for an exercise I had to implement a thread safe counter. This is what >> I came up with: >> >> [...] > > Could you try that: > > class ThreadSafe3Counter: Counter{ > private long counter; > private core.sync.mutex.Mutex mtx; > > public this() shared{ > mtx = cast(shared)( new core.sync.mutex.Mutex ); > } > > void increment() shared { > (cast()mtx).lock(); > scope(exit){ (cast()mtx).unlock(); } > > core.atomic.atomicOp!"+="(this.counter, 1); > } > > long get() shared { > return counter; > } > } > > > Unfortunately, there are some stupid design decisions in D about > "shared", and some people does not want to accept them. > > Example while you are using mutex, so you shouldn't be forced to use > atomicOp there. As a programmer, you know that it will be protected > already. That is a loss of performance in the long run. thanks for the implementation. i think this is nicer, than using __gshared. i think using atomic operations and mutexes at the same time, does not make any sense. one or the other.
thanks, Christian