Something needs to happen with shared, and soon.

Jacob Carlborg doob at me.com
Mon Nov 12 06:11:34 PST 2012


On 2012-11-12 12:55, Regan Heath wrote:
> 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.

I'm just throwing it in here again, AST macros could probably solve this.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list