How come a thread can access another thread's stack variable?

Simen Kjærås simen.kjaras at gmail.com
Fri Nov 25 08:24:39 PST 2011


On Fri, 25 Nov 2011 10:34:12 +0100, Christophe  
<travert at phare.normalesup.org> wrote:

> 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).

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.


More information about the Digitalmars-d-learn mailing list