Passing a value by reference to function in taskPool
Sparsh Mittal
sparsh0mittal at gmail.com
Fri Mar 1 18:51:54 PST 2013
Here is a code:
import std.stdio, std.datetime, std.random, std.range,
std.parallelism;
enum long numberOfSlaves = 2;
void myFunc( ref long countvar)
{
countvar = 500;
writeln( " value of countvar is ", countvar);
}
void main()
{
long count1=0, count2=0;
alias typeof(task!(myFunc)(0L)) MyTask ;
//Possibility 1
MyTask[numberOfSlaves] tasks;
tasks[0] = task!(myFunc)(count1);
taskPool.put(tasks[0]);
tasks[1] = task!(myFunc)(count2);
taskPool.put(tasks[1]);
for (long cc =0; cc < numberOfSlaves; cc++)
tasks[cc].yieldForce();
//Possibility 2
//myFunc(count1);
//myFunc(count2);
writeln( " value of count1 and count2 are ", count1, " ",
count2);
}
Possibility 1: Here, I wanted to pass a value by reference to
myFunc, but when I read that value in main function, its value is
not changed at all?
Possibility 2: It does what I want.
So, how to properly use the taskPool, so that pass by reference
works.
Uncomment/comment Possibility 1 or 2 to see the output.
More information about the Digitalmars-d-learn
mailing list