www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Broken TLS?

reply Dechcaudron <no-reply no-email.com> writes:
I keep getting data from within a struct shared across threads 
for apparently no reason: the code is the following:

import std.stdio;
import std.concurrency;
import core.thread : Thread, thread_joinAll;

struct Foo
{
     int a;
     int b;

     this(int a, int b)
     {
         this.a = a;
         this.b = b;

         writefln("Constructor -> a: %s, b: %s, &a is %s, &b: %s, 
this: %s, Thread: %s",
          this.a, this.b, &this.a, &this.b, &this, 
Thread.getThis.id);
     }

     ~this()
     {
         writefln("Final values were a:%s, b:%s", a, b);
     }

     void ping() shared
     {
         import core.time: dur;

         writefln("a: %s, b: %s, &a: %s, &b: %s, this: %s, Thread: 
%s",
          a, b, &a, &b, &this, Thread.getThis.id);

         a = 0;
         b = 0;
     }

     void fire()
     {
         spawn(&explode);
     }

     void explode() shared
     {
         ping();
     }
}

void main()
{
     auto a = Foo(1, 2);
     a.fire();

     thread_joinAll();
}

What I get on screen is the following:
Constructor -> a: 1, b: 2, &a is 7FFD5D9E3928, &b: 7FFD5D9E392C, 
this: 7FFD5D9E3928, Thread: 139774191032448
a: 1, b: 2, &a: 7FFD5D9E3928, &b: 7FFD5D9E392C, this: 
7FFD5D9E3928, Thread: 139774178481920
Final values were a:0, b:0

So effectively, the thread spawned does have access to the 
original struct and is able to modify it.

Is there anything I'm doing wrong? I won't lie, data sharing is 
the only thing about D I don't find quite usable yet. Can anybody 
help me out on this?
Jul 27 2016
parent reply ag0aep6g <anonymous example.com> writes:
On 07/27/2016 09:19 PM, Dechcaudron wrote:
 struct Foo
 {
[...]
     void ping() shared
     {
[...]
     }

     void fire()
     {
         spawn(&explode);
     }

     void explode() shared
     {
         ping();
     }
 }

 void main()
 {
     auto a = Foo(1, 2);
     a.fire();

     thread_joinAll();
 }
[...]
 Is there anything I'm doing wrong? I won't lie, data sharing is the only
 thing about D I don't find quite usable yet. Can anybody help me out on
 this?
I think the program should not compile. You can't call a shared method on an unshared struct/class, so you shouldn't be able to take make a delegate of it and call that. Reduced code: ---- struct Foo { void ping() shared {} } void main() { Foo a; // a.ping(); // rejected (&a.ping)(); // accepted } ---- We can also break immutable/const with this: ---- struct Foo { int x = 0; void ping() { x = 1; } } void main() { immutable Foo a; // a.ping(); // rejected (&a.ping)(); // accepted assert(a.x == 0); // fails } ---- Looks pretty bad. There's an open issue on this: https://issues.dlang.org/show_bug.cgi?id=16095
Jul 27 2016
parent reply Dechcaudron <no-reply no-email.com> writes:
On Wednesday, 27 July 2016 at 20:54:00 UTC, ag0aep6g wrote:
 Looks pretty bad. There's an open issue on this: 
 https://issues.dlang.org/show_bug.cgi?id=16095
Giving my 20 votes to the issue (are votes even taken into account?). At least now I know the source of attribute-enforcements breakdown is basically delegate management. That should help me out enough so I don't have this issue anymore. Thanks a bunch. Can I get your votes on that issue?
Jul 27 2016
parent ag0aep6g <anonymous example.com> writes:
On 07/28/2016 12:38 AM, Dechcaudron wrote:
 Giving my 20 votes to the issue (are votes even taken into account?). At
 least now I know the source of attribute-enforcements breakdown is
 basically delegate management. That should help me out enough so I don't
 have this issue anymore.

 Thanks a bunch. Can I get your votes on that issue?
As far as I know, no one cares much about votes. I'm not going to bother with them. This being a pretty serious accepts-invalid bug, it needs fixing regardless of votes. And unfortunately there are even more important/urgent bugs. There are 37 open dmd regressions [1], and 158 open wrong-code bugs [2]. Maybe not all of them are more important than the bug at hand, but a good bunch probably are. If voting did anything, I'd put everything into issues 15862 [3] and 16292 [4]. 15862 is a ridiculous wrong-code bug with pure nothrow functions. 16292 is a rejects-valid regression that is a blocking a toy project of mine (already has a pull request). [1] https://issues.dlang.org/buglist.cgi?bug_severity=regression&component=dmd&list_id=209764&query_format=advanced&resolution=--- [2] https://issues.dlang.org/buglist.cgi?component=dmd&keywords=wrong-code&keywords_type=allwords&list_id=209765&query_format=advanced&resolution=--- [3] https://issues.dlang.org/show_bug.cgi?id=15862 [4] https://issues.dlang.org/show_bug.cgi?id=16292
Jul 27 2016