Hello,
I havent done much C# over the last years, so you can imagine that when I land upon Parallel.For I am a bit lost. The arguments start with two numbers separated by commas, looks like the limits, then comes
a lambda expression
() => new Locals { TheArray = new uint[arraySize], SrcArray = new uint[arraySize] },
and then (after that comma)
delegate(int y, ParallelLoopState state, CombineLocals locals)
and then a block inside {}
and finally
, locals => { });
I've uploaded the code
Now here are some questions: Im using the C# 5.0 in a nutshell as my guide and it just talks of a Parallel for loop as having the following syntax:
Parallel.For(from, to, Function)
now the above code doesnt really fit into that: there is a lambda expression and then what seems to be an anonymous function that begins wth a delegate(...). It looks like these would be executed sequentially, but I'm not sure. Am I right on this point? The second point is that there seems to be a lambda followed by an anonymous function. I thought anonymous functions had been made redundant by lambda functions. Am I wrong?
Finally what is locals=>{} doing here??
Thankyou for your attenion.
Loading
VulpesPosted May 16, 2014, 10:05 AM
Henri de FeraudyPosted May 16, 2014, 10:03 AM
That's an excellent reply to my question!
Now I am going to have to figure out how to render this sequential because I am trying to port this code to C++.
VulpesPosted May 16, 2014, 9:39 AM
http://msdn.microsoft.com/en-us/library/dd783299(v=vs.110).aspx
As you surmised, the first two arguments are the start index (inclusive) and the end index (exclusive) of the iteration.
The third argument is a delegate which takes no arguments and returns a Locals object. A lambda expression has been used for this.
The fourth argument is a delegate which takes arguments of type int, ParallelLoopState and CombineLocals (presumably derived from Locals) and returns a Locals object. An anonymous method has been used for this.
Finally, the fifth argument is a delegate which takes an argument of type Locals but has no return value. A lambda expression has been used for this.
Although you're right that lambda expressions have to some extent superseded anonymous methods, there are some programmers who still like to use the latter when the body consists of several statements rather than just an expression.
As the MSDN link explains what these 3 delegates do better than I could, I'll leave you to read the description.
... and they said this stuff made concurrent programming easier :)