Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Software Transactional Memory

Software Transactional Memory

Lightening talk on STM with examples in Clojure and C, for university.

Marek Kubica

July 19, 2011
Tweet

More Decks by Marek Kubica

Other Decks in Education

Transcript

  1. Software Transactional Memory
    Marek Kubica
    TU München
    19.07.2011
    This work is licensed under the Creative Commons Attribution 3.0 License.
    Marek Kubica Software Transactional Memory

    View Slide

  2. Problem?
    Concurrent writes in shared memory
    T1 reads value
    T1 computes new value
    T2 reads value
    T1 writes new value
    T2 computes new value
    T2 overwrites value of T1
    Marek Kubica Software Transactional Memory

    View Slide

  3. How bad is it?
    An example program in C using pthreads
    $ ./broken 1 1000
    Number = 1000
    $ ./broken 2 1000
    Number = 997
    $ ./broken 15 1000
    Number = 2408
    $ ./broken 15 1000
    Number = 3228
    In short: computation results are nearly random.
    Marek Kubica Software Transactional Memory

    View Slide

  4. Solutions
    Typical real-world solutions
    Locking (Mutexes, Semaphores, Spinlocks)
    Message Passing (MPI)
    Problems
    Deadlocks (Dining philosopher problem)
    Complex programs
    Marek Kubica Software Transactional Memory

    View Slide

  5. Software Transactional Memory
    Concept
    All writes run in transactions
    If a value in the transaction was modied, the transaction is
    rolled back
    Transactions get repeated if they don't succeed
    Optimistic model, ecient with infrequent writes
    Marek Kubica Software Transactional Memory

    View Slide

  6. Fixed version
    (def number (ref 0))
    (def add1 (partial + 1))
    (def num-threads (Integer/parseInt (first *command-line-args*)))
    (def increments (Integer/parseInt (second *command-line-args*)))
    (defn add-number [field times]
    (dorun (repeatedly times
    (fn []
    (dosync
    (alter field add1))))))
    (def thread (partial add-number number increments))
    (printf "Starting %d threads, each with %d increments\n"
    num-threads increments)
    (dorun (apply pcalls (repeat num-threads thread)))
    (printf "Number = %d\n" @number)
    (shutdown-agents)
    Marek Kubica Software Transactional Memory

    View Slide

  7. Why does it work?
    Optimistic model
    Runs computation, tries to write
    Succeeds or gets rolled back
    Ecient implementation: use data structures with O(1)
    rollback
    Increased concurrency
    Pessimistic model
    Wait for lock, no computation
    Decreased concurrency
    Marek Kubica Software Transactional Memory

    View Slide

  8. Drawbacks
    Problems of STM
    Transactions sometimes hard to get right (inconsistent state)
    Language support not always great
    Increased bandwidth usage due to retried transactions
    Marek Kubica Software Transactional Memory

    View Slide

  9. Sources & Questions
    Source code
    Minied C code on
    the left. Compile
    with gcc -o
    broken broken.c
    -pthread
    Questions?
    And thank you for
    listening.
    Marek Kubica Software Transactional Memory

    View Slide