www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How come a thread can access another thread's stack variable?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
import core.thread;
import std.stdio;

struct Foo
{
    int field;
    void test()
    {
        writeln("field: ", &field);
    }
}

void main()
{
    Foo foo;
    auto thread = new Thread(&foo.test);
    thread.start();
    thread.join();
    foo.test();
}

This prints the same addresses. std.concurrency won't let you do this,
while std.parallelism uses some form of "weaker isolation", and it
seems core.thread has the same weaker isolation principle.

If "foo" is in TLS, how come a new thread can access its members?
Nov 22 2011
next sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Tue, 22 Nov 2011 23:00:17 +0100, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 import core.thread;
 import std.stdio;

 struct Foo
 {
     int field;
     void test()
     {
         writeln("field: ", &field);
     }
 }

 void main()
 {
     Foo foo;
     auto thread = new Thread(&foo.test);
     thread.start();
     thread.join();
     foo.test();
 }

 This prints the same addresses. std.concurrency won't let you do this,
 while std.parallelism uses some form of "weaker isolation", and it
 seems core.thread has the same weaker isolation principle.

 If "foo" is in TLS, how come a new thread can access its members?
You are passing a delegate that contains the pointer to the Foo you have put on the stack. What would you expect to happen? foo is not in tls, it is on the stack (which is also thread-local, but not considered tls). Passing an object on the stack to another thread is almost never safe, as the stack may unwind before the thread is done with the object. If you want to do this with tls, make a static/global variable with a Foo, and operate on it directly instead of passing the object between threads: import core.thread; import std.stdio; struct Foo { int field; } Foo foo; void test() { writeln("field: ", &foo.field); } void main() { auto thread = new Thread(&test); thread.start(); thread.join(); test(); }
Nov 22 2011
prev sibling parent reply travert phare.normalesup.org (Christophe) writes:
Andrej Mitrovic , dans le message (digitalmars.D.learn:30764), a écrit :
 import core.thread;
 import std.stdio;
 
 struct Foo
 {
     int field;
     void test()
     {
         writeln("field: ", &field);
     }
 }
 
 void main()
 {
     Foo foo;
     auto thread = new Thread(&foo.test);
     thread.start();
     thread.join();
     foo.test();
 }
 
 This prints the same addresses. std.concurrency won't let you do this,
 while std.parallelism uses some form of "weaker isolation", and it
 seems core.thread has the same weaker isolation principle.
 
 If "foo" is in TLS, how come a new thread can access its members?
It could with a cast to shared, couldn't it ? A fix would be to make Thread check the shared-ness of context pointer of the delegate (which may have to be fixed at the compiler level). -- Christophe
Nov 25 2011
parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
On Fri, 25 Nov 2011 10:34:12 +0100, Christophe  =

<travert phare.normalesup.org> wrote:

 Andrej Mitrovic , dans le message (digitalmars.D.learn:30764), a =C3=A9=
crit :
 import core.thread;
 import std.stdio;

 struct Foo
 {
     int field;
     void test()
     {
         writeln("field: ", &field);
     }
 }

 void main()
 {
     Foo foo;
     auto thread =3D new Thread(&foo.test);
     thread.start();
     thread.join();
     foo.test();
 }

 This prints the same addresses. std.concurrency won't let you do this=
,
 while std.parallelism uses some form of "weaker isolation", and it
 seems core.thread has the same weaker isolation principle.

 If "foo" is in TLS, how come a new thread can access its members?
It could with a cast to shared, couldn't it ? A fix would be to make Thread check the shared-ness of context pointer=
 of the delegate (which may have to be fixed at the compiler level).
It would, but core.thread is not supposed to do that. It's the unsafe, pedal-to-the-metal layer, and shouldn't stop people from doing stupid things.
Nov 25 2011