Trying to understand how "shared" works in D

Sparsh Mittal sparsh0mittal at gmail.com
Thu Jan 31 08:25:53 PST 2013


I wrote this code. My purpose is to see how shared works in D. I 
create a global variable (globalVar) and access it in two 
different threads and it prints fine, although it is not shared. 
So, can you please tell, what difference it makes to use/not-use 
shared (ref. 
http://www.informit.com/articles/article.aspx?p=1609144&seqNum=3).

Also, is a global const implicitly shared?


#!/usr/bin/env rdmd

import std.stdio;
import std.concurrency;
import core.thread;

const int globalConst = 51;
int globalVar = 17;
void main() {

   writefln("Calling Function");
     spawn(&test1, thisTid);
     writefln("Wait Here ");
     spawn(&test2, thisTid);
     writefln("End Here ");
}

void test1(Tid owner)
{
   writefln("The value of globalConst here is %s", globalConst);
   writefln("The value of globalVar here is %s", globalVar);
}

void test2(Tid owner)
{
   writefln("The value of globalConst here is %s", globalConst);
   writefln("The value of globalVar here is %s", globalVar);
}


More information about the Digitalmars-d-learn mailing list