Concurrency.
Jonathan M Davis
jmdavisProg at gmx.com
Mon Nov 28 01:49:02 PST 2011
On Monday, November 28, 2011 08:48:20 Debdatta wrote:
> Jude,
>
> Glad to hear that you found my post informative. Anyway, from what I
> understand, shared is infectious. However, lets say we have the following:
>
> class A
> {
> .......
>
> };
> shared class B
> {
> constructor()
> {
> myA = new A;
> }
> A myA;
> }
>
> This will create a problem as the myA is shared, whereas A is not. Hence, we
> have to tag A to be shared as well. This will lead to most things marked
> shared and will cause confusion. Not to mention that default sharing is the
> convention most programmers have been born with, and thats hard to change
> in a person. :D
>
> However, in default shared languages, hiding types is easy. For instance, if
> I wanted a thread with a completely local context, I would do this:
>
> class myLocalClass : Thread
> {
> public:
> //whatever I want accessible by other classes go here.
>
> void run();
>
> private:
>
> //whatever I want to make specific to my thread will be located here.
>
> }
>
> This seems to be a more elegant solution to the problem. Any thoughts?
I would point out that there's no such thing as a shared class. An _instance_
of a class can be shared, but not the class itself. It's the member functions
which are shared or not. Marking the class as shared just marks _everything_
in the class as shared. It's possible to have class which can be instantiated
as both shared and unshared. However, it leads to a fair bit of code
duplication (or the use of template or string mixins to avoid code
duplication), since every function that you want to be able to call with both
a shared and non-shared instance must be duplicated (one function being marked
shared, the other not).
However, the idea is that you'll generally design a class to be used as either
shared or non-shared, and that can have large ramifications on how stuff works
(e.g. whether a class is synchronized or whether mutexes or synchronized
blocks are used), so the implementations are often _expected_ to be different.
Overall, it's expected that the use of shared will be relatively rare, and the
language design reflects that, so it's likely to to harder to use in cases
where you want to use shared heavily. There have been several recent
discussions on various issues with shared. So, you might want to look through
the archives.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list