Deep C# - The Perils of the C# Parallel For
Written by Mike James   
Sunday, 15 December 2024
Article Index
Deep C# - The Perils of the C# Parallel For
Shopping Problems - Race Condition
Accessing Earlier Results

Making parallel code easier to use is an important development, but making it easier also means you can use it without realising what you are getting into. Unless we can find a way of making parallel code both easy and safe you are still going to have to take care - even with the easy-to-use Parallel For. Find out more in this extract from my book, Deep C#: Dive Into Modern C#.

Deep C#

 Buy Now From Amazon

DeepCsharp360

 Chapter List

  1. Why C#?
    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference
  5. Structs & Classes
       Extract
    Structs & Classes 
  6. Inheritance
      
    Extract
    Inheritance
  7. Interfaces & Multiple Inheritance
      
    Extract Interface
  8. Controlling Inheritance
    II Casting & Generics
  9. Casting - The Escape From Strong Typing
      
    Extract Casting I
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic
    Typing
    III Functions
  13. Delegates
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures
    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For ***NEW!
    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions
    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

Extra Material

 <ASIN:1871962714>

 <ASIN:B09FTLPTP9>

So far in our exploration of asynchronous code we have considered parallel execution without necessarily executing anything at the same time. Today’s processors, however, have multiple cores and this means that it is possible to actually run threads in parallel and so hopefully speed things up. Asynchronous code is mostly about keeping the UI responsive by dividing the processor’s attention between servicing the UI and doing useful work. True parallelism has the attraction of speeding up the useful work and thus is attractive even without considering the responsiveness of the UI.

The .NET parallel extensions are great. They are easy to use and provide a huge payback – but, and this is a big but, parallel programming isn’t easy. If it was we would be using it all over the place and the parallel extensions would be nothing new.

Making parallel code easier to use is an important development, but making it easier also means you can use it without realizing what you are getting into. By being easy to use, Parallel.For encourages programmers who might never consider adding additional threads to their code simply because it promises to get the job done faster. Unless we can find a way of making parallel code both easy and safe you are still going to have to take care.



Last Updated ( Sunday, 15 December 2024 )