Threads, shread and TLS

Steven Schveighoffer schveiguy at yahoo.com
Fri Jan 7 10:20:45 PST 2011


On Fri, 07 Jan 2011 11:30:17 -0500, Adam Conner-Sax  
<adam_conner_sax at yahoo.com> wrote:

> Thanks!
>
> It's not really about correctness as much as trying to understand how  
> these
> different storage classes work.  I understand that there is only one foo  
> object.
> I wanted to see which parts are thread-local and which are shared and  
> how the
> constructors work.

A class instance can be shared or unshared.  Either way, changing the data  
on the same instance updates the same instance, there is not a copy of the  
whole world in each thread, just a copy of the thread local storage block.

So you are sort of conflating 'per instance' with 'per thread.'

int x -- per instance (shared or not)
static int x -- per thread (in the TLS block)
shared static int x -- per process, not in any instance.

>
> I'm working (way over my head) on a more complex threading issue and I  
> realized
> that I didn't quite understand the storage classes and constructors. Now  
> I get it
> a bit more.
>
> By "untagged sharing" do you mean the "a" variable which is shared in  
> the sense
> that all threads see the same copy but does not have storage class  
> "shared"?  Yes,
> that confuses me too.  Should it be an error?

Yes, if you have a piece of data that shared and not marked with __gshared  
or shared, then we have a problem.  The problem is that a lot of code  
assumes that situation cannot happen without casts (you can always cast  
and override the type system), so you can make assumptions based on that.   
For example, a lot of C++/java code is written *just in case* an object is  
shared.  With D, the hope is that you are *guaranteed* that it is shared  
or not, so you can optimize your code that way (i.e. use a lock or not).   
As long as the possibility exists that code not marked as shared can be  
easily shared without a cast, we cannot make those assumptions.

I think it should be filed as a bug, but I'm not sure if someone's already  
reported it.

>
> I have never dealt with bugzilla but I will try to figure out how to do  
> what you
> ask :)

Go to http://d.puremagic.com/issues/enter_bug.cgi

You will have to create a user before filing a bug, but bugzilla is  
relatively straightforward to use.

-Steve


More information about the Digitalmars-d-learn mailing list