digitalmars.D.learn - Passing a value by reference to function in taskPool
- Sparsh Mittal (34/34) Mar 01 2013 Here is a code:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (14/17) Mar 01 2013 This is a known issue and is documented in the std.parallelism module:
- Sparsh Mittal (1/1) Mar 02 2013 Thanks a lot for your reply.
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
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