On Friday, 17 July 2015 at 14:14:41 UTC, byron wrote:
Since I have yet to use or see anyone use shared in a useful way I avoid it.
It's one way to avoid it. So, you mean you always use send/receive
when you need threading?

I did small test to know the memory layout.

import core.atomic;
int foo;
shared int sfoo;
class DerivedThread : Thread {
    int count;
    shared int scount;
    this() {
        super(&run);
    }
    private void run() {
        inc();
writefln("at thread: foo=%s, &foo=%s, sfoo=%s, &sfoo=%s", foo, &foo, sfoo, &sfoo); writefln("at thread: count=%s, &count=%s, scount=%s, &scount=%s", count, &count, scount, &scount);
    }
    void inc() {
        ++foo;
        atomicOp!"+="(sfoo, 1);
        ++count;
        atomicOp!"+="(scount, 1);
    }
}
void main() {
    auto thr = new DerivedThread();
    thr.start();
    thr.inc();
    thr.join();
writefln(" at main: foo=%s, &foo=%s, sfoo=%s, &sfoo=%s", foo, &foo, sfoo, &sfoo); writefln(" at main: count=%s, &count=%s, scount=%s, &scount=%s", thr.count, &thr.count, thr.scount, &thr.scount);
}

at thread: foo=1, &foo=A33580, sfoo=2, &sfoo=5541E4
at thread: count=2, &count=240178, scount=2, &scount=24017C
  at main: foo=1, &foo=984B28, sfoo=2, &sfoo=5541E4
  at main: count=2, &count=240178, scount=2, &scount=24017C

That means Thread object "thr" is always shared while
global foo is not shared because it has different memory location.
In my understanding, shared is not only checking something at compile time,
but also it affects on the memory layout.

Aki.

Reply via email to