www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - New scope modifier "unshared" for temporary ownership of shared

reply Marco Leise <Marco.Leise gmx.de> writes:
Since only functions performing a single operation on a
single shared operand can accept shared variables, it is
practically impossible to use shared mutable data in
generic algorithms.

Assuming that with this premise user code will always use a
mutex to secure access to shared data, developers would end up
with many casts from shared to unshared.
So here is UNSHARED, a new scope modifier:

synchronized(mutex) unshared(this.count, this.list)
{
    // do something with the now unshared count and list
}

This puts the mutex and the variables protected by it in
relation in the code and removes superfluous casts.

-- 
Marco
Apr 09 2014
parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 9 April 2014 at 11:36:15 UTC, Marco Leise wrote:
 Since only functions performing a single operation on a
 single shared operand can accept shared variables, it is
 practically impossible to use shared mutable data in
 generic algorithms.

 Assuming that with this premise user code will always use a
 mutex to secure access to shared data, developers would end up
 with many casts from shared to unshared.
 So here is UNSHARED, a new scope modifier:

 synchronized(mutex) unshared(this.count, this.list)
 {
     // do something with the now unshared count and list
 }

 This puts the mutex and the variables protected by it in
 relation in the code and removes superfluous casts.

We don't need new qualifier for that, implementing `scope` will do the trick among other tricks. Locking can safely cast shared into scope without breaking type system. And you will need same safety checks for implementation of "unshared" anyway (prohibiting storing references)
Apr 09 2014