www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - To share or not to share, this is the question.

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
The shared keyword makes it's user regret ever trying to use it.
This keyword has a single purpose: it generates frustration at compile-time.
Enough with the emotions.
I'm trying to make a Win32 multi-threaded warpper, which allows to
efficiently create Win32 windows, that immediately start processing
their messages in a separate thread (which will eventually get
migrated into a thread pool).
As i was trying to deal with the shared-ness, i found out about this:

shared class T
{
	alias void* v;
	static assert(is(v == shared));
}

this fails the static assert. I don't see any point of this. Why on
earth would one want a non-shared nested type in a shared class?
By far the most frustrating thing about this is the fact, that the
supposedly great std.concurrency.spawn function is completely useless,
because it fails it's "no local aliases" assert, when i pass _this_ in
the function, which is of a shared class type.
This happends only when that class has a HWND member. If i replace the
HWND with an int, it starts working.
My second question is: why the deuce isn't HWND member shared too
inside my class?

I'm sorry for the language. It's just that _shared_ keyword is doing
it's job very nicely: it generates a compile-time frustration REALLY
big, REALLY fast.
Also, it seems, that DMD isn't aware, that any extern(C) automatically
implies _gshared parameters for functions and members for structs,
which would automatically remove all problems with shared-ness in C
code. This is even more frustrating because now i need to cast
everything to non-shared.
Oct 17 2011
next sibling parent reply Kagamin <spam here.lot> writes:
Gor Gyolchanyan Wrote:

 My second question is: why the deuce isn't HWND member shared too
 inside my class?
It should not be shared. And you shouldn't ever pass HWND across threads.
Oct 17 2011
next sibling parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
The point is not in the HWND itself, but in the fact, that a shared
class has a non-shared member. Isn't shared supposed to be transitive?

On Mon, Oct 17, 2011 at 2:55 PM, Kagamin <spam here.lot> wrote:
 Gor Gyolchanyan Wrote:

 My second question is: why the deuce isn't HWND member shared too
 inside my class?
It should not be shared. And you shouldn't ever pass HWND across threads.
Oct 17 2011
parent reply Kagamin <spam here.lot> writes:
Gor Gyolchanyan Wrote:

 The point is not in the HWND itself, but in the fact, that a shared
 class has a non-shared member. Isn't shared supposed to be transitive?
It's transitive for data. Alias is not data.
Oct 17 2011
parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I don't get it. HWND is an alias for void*. void* is data. what do you
mean, "alias is not data"?


On Mon, Oct 17, 2011 at 3:50 PM, Kagamin <spam here.lot> wrote:
 Gor Gyolchanyan Wrote:

 The point is not in the HWND itself, but in the fact, that a shared
 class has a non-shared member. Isn't shared supposed to be transitive?
It's transitive for data. Alias is not data.
Oct 17 2011
parent reply "Robert Jacques" <sandford jhu.edu> writes:
On Mon, 17 Oct 2011 08:28:08 -0400, Gor Gyolchanyan
<gor.f.gyolchanyan gmail.com> wrote:
 I don't get it. HWND is an alias for void*. void* is data. what do you
 mean, "alias is not data"?
void* is a type, not a member field of the class (data)
Oct 18 2011
parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I meant, i have a member of type HWND.

On Wed, Oct 19, 2011 at 6:47 AM, Robert Jacques <sandford jhu.edu> wrote:
 On Mon, 17 Oct 2011 08:28:08 -0400, Gor Gyolchanyan
 <gor.f.gyolchanyan gmail.com> wrote:
 I don't get it. HWND is an alias for void*. void* is data. what do you
 mean, "alias is not data"?
void* is a type, not a member field of the class (data)
Oct 18 2011
parent "Robert Jacques" <sandford jhu.edu> writes:
On Wed, 19 Oct 2011 02:06:06 -0400, Gor Gyolchanyan
<gor.f.gyolchanyan gmail.com> wrote:
 I meant, i have a member of type HWND.

 On Wed, Oct 19, 2011 at 6:47 AM, Robert Jacques <sandford jhu.edu> wrote:
 On Mon, 17 Oct 2011 08:28:08 -0400, Gor Gyolchanyan
 <gor.f.gyolchanyan gmail.com> wrote:
 I don't get it. HWND is an alias for void*. void* is data. what do you
 mean, "alias is not data"?
void* is a type, not a member field of the class (data)
And shared class T { alias void* v; v v2; static assert(is(typeof(v2) == shared)); } compiles so what's your problem?
Oct 19 2011
prev sibling parent Kagamin <spam here.lot> writes:
Kagamin Wrote:

 Gor Gyolchanyan Wrote:
 
 My second question is: why the deuce isn't HWND member shared too
 inside my class?
It should not be shared. And you shouldn't ever pass HWND across threads.
Huh, well, looks like HWND can be shared, see e.g. PostMessage function http://msdn.microsoft.com/en-us/library/ms644944%28VS.85%29.aspx but it seems there's quite restricted set of functions which can deal with a shared handle.
Oct 17 2011
prev sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 17.10.2011 14:24, Gor Gyolchanyan wrote:
 The shared keyword makes it's user regret ever trying to use it.
 This keyword has a single purpose: it generates frustration at compile-time.
 Enough with the emotions.
 I'm trying to make a Win32 multi-threaded warpper, which allows to
 efficiently create Win32 windows, that immediately start processing
 their messages in a separate thread (which will eventually get
 migrated into a thread pool).
 As i was trying to deal with the shared-ness, i found out about this:

 shared class T
 {
 	alias void* v;
I guess the problem is here: v is void * and not shared at all. Alias is not affected by outer shared decl, shared applies only to members: fields and functions. In a sense, an alias is merely using the same name scope. Imagine if you semantics (i.e. shared affects aliases) worked, then how would you declare a *not* shared type alias inside of it ? This won't work: alias Unqual!(void*) v; -- Dmitry Olshansky
Oct 17 2011
parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Ok, but if i declared a member of type v it should be shared, right?
since i'd essentially declare it to be void*, which gets the enclosing
class's shared attribute.
This is what i did for a class, that contains a HWND member. But the
std.concurrency.spawn function flipped me off and said, that i'm not
supposed to send over an object with unshared aliases.
What's the logic behind this? the HWND should be shared.

I know, i already found out, that HWND is strictly single-threaded,
but the point here is to understand why does the spawn function think
it's unshared.

On Mon, Oct 17, 2011 at 4:45 PM, Dmitry Olshansky <dmitry.olsh gmail.com> w=
rote:
 On 17.10.2011 14:24, Gor Gyolchanyan wrote:
 The shared keyword makes it's user regret ever trying to use it.
 This keyword has a single purpose: it generates frustration at
 compile-time.
 Enough with the emotions.
 I'm trying to make a Win32 multi-threaded warpper, which allows to
 efficiently create Win32 windows, that immediately start processing
 their messages in a separate thread (which will eventually get
 migrated into a thread pool).
 As i was trying to deal with the shared-ness, i found out about this:

 shared class T
 {
 =A0 =A0 =A0 =A0alias void* v;
I guess the problem is here: v is void * and not shared at all. Alias is =
not
 affected by outer shared decl, shared applies only to members: fields and
 functions. In a sense, an alias is merely using the same name scope.
 Imagine if you semantics (i.e. shared affects aliases) worked, then how
 would you declare a *not* shared type alias inside of it ?
 This won't work:
 =A0 =A0 =A0 =A0alias Unqual!(void*) v;

 --
 Dmitry Olshansky
Oct 17 2011