New to D - playing with Thread and false Sharing
tony288 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 20 13:01:56 PDT 2015
On Thursday, 20 August 2015 at 15:37:35 UTC, Dejan Lekic wrote:
> Keep in mind that in D everything is thread-local by default! :)
> For shared resources use __gshared or shared (although I do not
> know for sure whether shared works or not).
Thanks, I changed the code and the previous one was already using
shared.
import std.stdio;
import core.time;
import core.thread;
void main() {
MonoTime before = MonoTime.currTime;
runTest();
MonoTime after = MonoTime.currTime;
Duration timeElapsed = after - before;
writeln("Duration is " , timeElapsed) ;
}
private static void runTest()
{
Thread[] threads = new Thread[4];
FalseSharing[] fs = new FalseSharing[4];
for (int i = 0; i < threads.length; i++){
FalseSharing fsx = new FalseSharing(i);
fs[i] = fsx;
threads[i] = fsx;
}
foreach(Thread t ; threads){//starts all threads
t.start();
}
foreach(Thread t; threads){
t.join();
}
}
static PaddingClass[] longs = new PaddingClass[4];
class FalseSharing : Thread
{
private const int threadIndex;
this(const int index)
{
threadIndex = index;
longs[threadIndex] = new PaddingClass();
super(&run);
}
void run()
{
long max = 1000L * 100L * 100L * 10L;
for(long i=1; i<=max ; i++){longs[threadIndex].value =
i;}
}
}
public static shared class PaddingClass
{
public double p1,p2,p3,p4,p5,p6;//padding.
public shared long value = 0L; //shared across multiple
thread
}
So what I see, D ( of course and expected) using more or less the
same syntax as java. Behaves very differently. Which I mean
padding or not, D behaves slower than Java with padding by a long
way.
Now what I would like to know, how would I make this code more
efficient? Which is basically the aim I'm trying to achieve.
Any pointers would be really help full. Should I use
concurrency/parallelism etc..?
Thanks
More information about the Digitalmars-d-learn
mailing list