What is shared functions?

Steven Schveighoffer schveiguy at yahoo.com
Mon Oct 24 12:41:51 PDT 2011


On Mon, 24 Oct 2011 12:01:07 -0400, simendsjo <simendsjo at gmail.com> wrote:

> On 24.10.2011 17:23, Steven Schveighoffer wrote:
>> On Sun, 23 Oct 2011 08:32:34 -0400, simendsjo <simendsjo at gmail.com>  
>> wrote:
>>
>>> What does shared for functions mean? I thought it was supposed to
>>> automatically synchronize access, but this doesn't seem to be the case.
>>>
>>> void f() shared {
>>> // no synchronization
>>> }
>>>
>>> void f() {
>>> synchronized {
>>> // do stuff
>>> }
>>> }
>>
>> Shared functions do not affect the function. All they do is affect the
>> 'this' pointer.
>>
>> This:
>>
>> struct S
>> {
>> void f() shared {}
>> }
>>
>> is roughly equivalent to this:
>>
>> struct S
>> {}
>>
>> void f(shared ref Foo this){}
>>
>> -Steve
>
>
> So you cannot create a function to be called on both shared and unshared  
> instances? For mutable/immutable there's const, but there is no  
> "maybeShared".

No.  There isn't a hybrid that works as well as const does.  I suspect one
could be created, but it's very infrequent that you want to work with
shared data in the same way you want to work with unshared data.  With
immutable vs. immutable, const incurs no penalty.

But for shared vs. unshared, there is a very real penalty for obeying
shared semantics.

-Steve


More information about the Digitalmars-d-learn mailing list