AsParallel

This is a discussion on AsParallel within the C# Programming forums, part of the General Programming Boards category; Whats the advantage of using AsParallel on an array opposed to just using the array directly? Code: al.ToArray().AsParallel().ForAll(po => CacheToAppFabric((MyObject)po));...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    AsParallel

    Whats the advantage of using AsParallel on an array opposed to just using the array directly?

    Code:
    al.ToArray().AsParallel().ForAll(po => CacheToAppFabric((MyObject)po));

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Your operations in the ForAll will be parallelized. They will execute in parallel (probably dependent on your machine). That's why it's called "ForAll" and not "ForEach".
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Be careful when using AsParallel. Note that while it is thread safe it does not guarantee operations you may perform from within the LINQ statement are thread safe. Calling a method or extension from within the LINQ statement that is not thread safe invalidates the thread safety of it.

    AsParallel also does not guarantee your statement will run faster in all cases such as in the case of calling a query operator that has a lock within it. The lock will limit the LINQ statement's parallel efficiency.

    http://www.albahari.com/threading/
    Last edited by VirtualAce; 10-03-2012 at 05:40 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21