KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Is there any way to achieve the Parallel.For version of this for loop? for (int i = 0; i < 100; i += 2) { DoStuff(i); } I don't see an overload which accepts a step parameter, though I can't think of any reason this would be logically impossible. The accepted answer to this and this question suggests using Parallel.ForEach on a range of int s generated using Enumerable.Range , but in my case I am using thread local data so Parallel.ForEach is not an option . Another option is to just check if i % 2 == 0 in the body of my loop and return , but this still executes the thread local data intializer Func and finalizer Func . Below is a code snippet demonstrating this option: Parallel.For<Bar>(0, limit, () => new Bar(), //thread local data initialize (i, state, local) => //loop body { if (i % 2 != 0) return local; local.foo += DoStuff(i); return local; }, (local) => //thread local data post-action { lock (loopLocker) { globalData.foo += local.foo; ); } );
Tags (comma-separated)
Save Edits
Cancel