Something needs to happen with shared, and soon.

Regan Heath regan at netmail.co.nz
Mon Nov 12 03:55:51 PST 2012


On Mon, 12 Nov 2012 02:30:17 -0000, Walter Bright  
<newshound2 at digitalmars.com> wrote:
> To make a shared type work in an algorithm, you have to:
>
> 1. ensure single threaded access by aquiring a mutex
> 2. cast away shared
> 3. operate on the data
> 4. cast back to shared
> 5. release the mutex

So what we actually want, in order to make the above "nice" is a "scoped"  
struct wrapping the mutex and shared object which does all the "dirty"  
work for you.  I'm thinking..

// (0)
with(ScopedLock(obj,lock))  // (1)
{
   obj.foo = 2;              // (2)
}                           // (3)
// (4)

(0) obj is a "shared" reference, lock is a global mutex
(1) mutex is acquired here, shared is cast away
(2) 'obj' is not "shared" here so data access is allowed
(3) ScopedLock is "destroyed" and the mutex released
(4) obj is shared again

I think most of the above can be done without any compiler support but it  
would be "nice" if the compiler did something clever with 'obj' such that  
it knew it wasn't 'shared' inside the the 'with' above.  If not, if a full  
library solution is desired we could always have another temporary  
"unshared" variable referencing obj.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list