www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing a value by reference to function in taskPool

reply "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
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.
Mar 01 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/01/2013 06:51 PM, Sparsh Mittal wrote:

 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?
This is a known issue and is documented in the std.parallelism module: "BUGS: Changes to ref and out arguments are not propagated to the call site, only to args in this struct." You can pass a pointer: void myFunc(long *countvar) { *countvar = 500; writeln( " value of countvar is ", *countvar); } // ... tasks[0] = task!myFunc(&count1); Ali
Mar 01 2013
parent "Sparsh Mittal" <sparsh0mittal gmail.com> writes:
Thanks a lot for your reply.
Mar 02 2013