What is shared functions?

simendsjo simendsjo at gmail.com
Mon Oct 24 16:16:06 PDT 2011


On 24.10.2011 21:41, Steven Schveighoffer wrote:
>>
>> 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.

Perhaps infrequent, but I just encountered a use-case:

// Shared between all threads (created in module ctor)
shared Context defaultContext = {...}

// But each thread *could* create it's own context
class Context {
   Socket create() {...} // so this could be called on both shared and 
unshared instances. The resulting socket should always be thread local
}

As I don't have any experience with shared, this might have implications 
I don't know about though..


More information about the Digitalmars-d-learn mailing list