pass-by-ref semantics for structs (was Deque impl.)

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 31 15:14:49 PST 2013


On Thu, 31 Jan 2013 17:37:39 -0500, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> On Thu, 31 Jan 2013 17:31:38 -0500, Rainer Schuetze <r.sagitario at gmx.de>  
> wrote:
>
>> Any reference that is accessible from multiple threads, e.g. a global  
>> shared variable.
>>
>> shared(Container) allObjects;
>>
>> Without any thread having a reference to it, the payload's reference  
>> count is 1. The reading thread above increments the counter when making  
>> a local copy, but the writing thread decrements the counter when  
>> assigning Container.init. If both run concurrently, with the sequence 1  
>> to 3 above, trouble is inevitable.
>
> Right, but isn't that an issue with having ANY shared variable that  
> isn't protected by a lock?
>
> I was thinking you had passed the pointer in via a message or something.

BTW, couldn't we solve this with a function that increments the retain  
count, and then returns a pointer to the object?  Such a method would have  
to be atomic on shared instances.

-Steve


More information about the Digitalmars-d mailing list