Git Product home page Git Product logo

parallel-workshop's Introduction

parallel-workshop

Some exercises in writing performant parallel code in .Net, with an emphasis on: -

  • Avoiding locks where possible
  • Choosing appropriate locks where they are needed

The exercises are in the project ParallelWorkshop, with one exercise per folder. The ParallelWorkshopTests project contains an equivalent folder for each exercise, containing a few tests.

Typically, the code in the exercise is part-written and the test(s) show, by failing, one or more deficiencies of the code written so far. The exercise is to: -

  • Make the test(s) green (without changing test code)
  • Get experience of using different classes and techniques for parallel code

Among the .Net framework classes that should be considered are: -

Class Purpose
Interlocked Perform simple operations thread-safely, without locks
Volatile Perform reads and writes thread-safely, without locks
Monitor The classic .Net lock, used by the lock keyword. Allows only a single thread at a time to own the lock. Also allows basic signalling between threads. Supports re-entrance.
Lazy Initialise a value only when needed, and then only once. Choice of thread-safety levels.
LazyInitializer Initialise a value only when needed, and then only once. Unlike Lazy, does not involve instantiating an additional object.
ReaderWriterLock and ReaderWriterLockSlim Lock allowing multiple simultaneous reads, but only one simultaneous write (which cannot coincide with any read). Slower to enter and exit than Monitor, so use only when there is a real benefit to multiple simultaneous reads. Can support re-entrance, at additional cost.
ManualResetEventSlim A way to signal events between threads. Has some advantages over Monitor. Note that this is almost always better than the old ManualResetEvent class.
ConcurrentDictionary, and other concurrent collections Thread-safe collections, with special operations targetting parallel usage.
BlockingCollection Excellent class for producer-consumer pattern.
Barrier Co-ordinates several threads in reaching a certain point in an operation

If you there are any classes in the above list that you have never encountered before, it would be worth quickly reading their online documentation.

Each exercise has at least one pre-made possible solution, in a folder called PossibleSolution. These solutions certainly make the tests go green but they might not always be the best possible solution. See if you can do better, in terms of: -

  • Performance
  • Correctness
  • Clarity

parallel-workshop's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.