Multithreading 101

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    - thanks for coming – ensured my free entry to the conference :)

    Favorites, Groups & Events

    Multithreading 101 - Presentation Transcript

    1. Multithreading 101
        • Tim Penhey
    2. Contents
      • Terms and definitions
      • When to use threads, or not
      • Threading primitives
      • Threading concepts
        • Memory barriers
        • Race conditions
        • Priority inversion
      • Where to from here?
    3. Multitasking
      • Allows multiple programs to appear to be running at the same time
      • Context switch to move from one task to another
      • Two types of Multitasking Operating Systems
        • Co-operative
          • Application relinquishes control
        • Preemptive
          • OS kernel interrupts task
    4. Processes and Threads
      • Processes
        • Own memory
          • OS protected – GPF
        • Own resources
          • file handles, sockets, ...
      • Thread
        • Short for “thread of execution”
        • Every process has “main thread”
        • Additional threads for a process share resources
    5. Threading
      • Don't Do It
    6. I Mean It
      • Just say NO!
    7. Why Avoid Threads?
      • Threads add complexity
      • Subtle bugs that are hard to find
      • Debugging threads is hard
      • Often using a debugger can hide a race condition
      • Harder to test
    8. What Are The Alternatives?
      • Multiple processes
      • Just using one thread
      • Event based callbacks
    9. When To Use Threads
      • GUI responsiveness
      • Complete processor utilisation
      • Network connectivity
    10. GUI Responsiveness
      • User interfaces are event driven
      • If a long running function blocks events the GUI can become unresponsive
      • Repaint is a classic example
    11. Complete Processor Utilisation
      • When the following criteria meet
        • Problem can be broken into independent parts
        • Results needed as soon as possible
        • Problem is only computationally bound
      • One thread per (effective) CPU
        • Often called worker threads
      • Need an abstraction to separate work generation from the workers
        • Queues are popular
    12. Worker Threads
      • More threads than processors means that the extra threads are not being used effectively
      • Overhead of context switching reduces computation time
    13. Queues
      • Work generator thread is not handing work to a particular worker
      • An abstraction needed between producer and consumer
      • Often a queue – push on the back, pop from the front
      • When a worker is available, it tries to pop an item from the queue
    14. Network Connectivity
      • Communicating over a network adds a level of non-determinism
      • Client side threads
        • Useful when the client needs to do more than just wait for the response – such as GUI events
      • Server side threads
        • Server waits for connections
        • Socket descriptor represents connection can be used independently of waiting for more connections
    15. Threading Primitives
      • Threads
      • Atomic functions
      • Mutual exclusion blocks
      • Events / Conditions
      • Semaphores
    16. Common Thread Functionality
      • Create a thread
        • Given a function to execute
        • When that function is finished, so is the thread
      • Wait for a thread to finish
        • Either wait forever or wait for a time limit
      • Kill a thread
        • Not always available – Java
    17. Atomic Functions
      • Provided by the OS kernel
      • Guaranteed to complete before task is switched out
      • Examples:
        • increment and decrement
        • decrement and test
          • subtracts one and returns true if value now zero
        • compare and swap
          • three params, the variable, the expected value, the new value – only updates if current value is the expected one
    18. Mutual Exclusion Blocks
      • Primitive that provides interface to mutual exclusion blocks often referred to as a mutex
      • A mutex is owned by at most one thread
      • Attempts to acquire ownership of an already owned mutex will block the thread
      • Releasing an owned mutex make it available for other threads to acquire
    19. Common Mutex Functionality
      • create mutex
      • destroy mutex
      • acquire mutex
      • try to acquire mutex
      • release mutex
    20. Why Are Mutexes Needed?
      • Any non-trivial data structure uses multiple variables to define state
      • Thread may be switched out after only modifying part of the state
      • Other thread accessing the inconsistent state of the data structure is now in the world of undefined behaviour
    21. Mutex Example
      • There is a container of Foo objects called foo
      • A mutex to protect foo access called m
      • Thread 1
          • acquire mutex m
          • bar = reference to a value in foo
          • release mutex m
          • use bar...
      • Thread 2
          • acquire mutex m
          • modify foo
          • release mutex m
    22. Mutex Example Discussion
      • Mutual exclusion by itself does not ensure correct behaviour
      • It is more than just acquiring access to an object that needs to be protected
      • If the granularity of mutex use is too fine, it can have speed implications
      • Over zealous use of mutexes can lead to serialisation of access – sometimes to the level of rendering the threads useless
    23. Event / Condition
      • Win32 and Posix implementations differ
        • event on Win32
        • condition on Posix
      • A thread can wait on an event – this will cause the waiting thread to block (most of the time)
      • When the event is signalled , one or more threads (implementation dependant ) will wake and continue executing
    24. Event Example
      • A client / server system where the client can cancel long running requests
      • Client thread passes work request to a worker thread
      • Worker thread informs client when the work is complete
      • Client thread waits on an event
      • Worker thread signals the event
    25. Event Example II
      • Thread 1:
          • gets client request
          • passes on to worker thread
          • wait on event
      • Worker Thread:
          • gets work for client
          • processes for a long time
          • signals event when complete
      • Thread 1:
          • wakes on event signal
          • returns result to client
    26. Event Example III
      • Thread 1: as previous slide
      • Worker Thread:
          • gets work for client
          • processes for a long time
      • Thread 2:
          • client cancels original request
          • sets cancelled flag
          • signals event
      • Thread 1:
          • wakes on event signal
          • returns cancelled request
    27. Event Example IV
      • What to do with worker thread when cancelled ?
        • run to completion and discard result
        • if possible, interrupt worker prior to completion
      • General timeouts would also be possible using a “watchdog” thread
    28. Semaphore
      • A non-negative counter that can be waited on
      • Attempting acquisition of a semaphore will do one of two things:
        • Decrement the value and continue
        • Wait on the semaphore if it is zero
      • Releasing a semaphore increments the value
        • A thread waiting on the semaphore will be woken and will attempt to acquire the semaphore again
      • Used where there are multiple copies of a resource
    29. Database Connection Pool
      • Initiating database connections can be time consuming, so construct “ahead of time”
      • A semaphore is initialised with the number of connections
      • Requesting a connection is managed by acquiring the semaphore
        • If a connection is available it is returned
        • If none are available, thread blocks until one is
    30. Database Connection Pool II
      • A collection for the connections
      • A semaphore to wait on
      • A mutex to protect the state of the collection
      • What about a lock free implementation?
    31. Thread Local Storage
      • A memory block associated with a particular thread
      • Since thread local storage is not shared, protection from other threads is not needed
      • Access to thread local storage is through platform specific API
    32. Lock-free Data Structures and Wait-free Algorithms
      • Lock free data structures are written without mutexes
        • Designed to allow multiple threads to share data without corruption
        • Built using atomic functions
        • Well documented examples available for simple containers
      • Wait free algorithms are where the algorithm is guaranteed to finish in a finite number of steps regardless of other thread behaviour
    33. Queues Revisited
      • We have a work producer and four workers.
      • Work is going to be stored on a queue.
      • How can this be done?
      • What should a worker do if the queue is empty?
    34. Protecting a Queue
      • A mutex to protect the queue internals
      • An event so the workers can wait on empty
      • The producer can signal the event when adding work
      • or
      • Hand craft single linked list from atomic functions (aka Lock-Free)
    35. Threading Idioms
      • Producers and Consumers
        • work crew
      • Pipeline
      • Monitors
    36. Simple Question
      • x = 0, y = 0
      • Thread 1:
          • while x == 0 loop
          • print y
      • Thread 2:
          • y = 0xDEADBEEF
          • x = 1
      • What is printed?
    37. Smart Processors
      • Advances in processor efficiency have led to many tricks and optimisations
      • Execution out of order
        • Grouping reads and writes together
        • Doing block reads and writes
      • Grouping is done so transparent to a single thread of execution
    38. Memory Barriers
      • Process instruction that inhibits “optimal” reordering of memory access
      • Makes sure that all loads and saves before the barrier happen before loads and saves after
      • Many synchronisation primitives have implicit memory barriers
        • It is usual for a mutex to have a memory barrier on both acquisition and releasing
    39. Race Conditions
      • Anomalous behaviour due to unexpected critical dependence on the relative timing of events
        • Many can be solved through the use of synchronisation primitives
        • Some are problems at the design level
      • It is the unexpected nature of the problems that make writing multithreaded code hard
    40. Deadlock
      • Best known result of a race condition
      • Four necessary conditions for deadlock
          • Tasks claim exclusive control of the resources they require
          • Tasks hold resources already allocated while waiting for additional resources
          • Resources can not be forcibly removed from the tasks holding them
          • A circular chain of tasks exists such that each task holds one or more resources that are being requested by the next task in the chain
    41. Livelock
      • Two or more threads execute continuously but make no progress
        • Two threads both doing calculations while some value is true, but in such a way that each invalidates the other's test, causing both threads to continue for ever
      • Real world example
        • Two people walking towards each other in a corridor. Both decide to be polite and step to the side, however just end up mirroring the others movements
    42. Priority Inversion
      • More associated with real time systems
      • Occurs when a low priority task owns a resource that a high priority task needs
      • In this case the low priority task takes precedence over the high priority task
      • Made more complex if there is a medium priority task that is also running taking precedence over the low priority task
    43. The Near Future
      • Already we have desktop machines with multiple processors, and now multi-core
      • In order to get full use from machines, multithreading will be used more and more
      • Languages that have not defined a thread aware memory model (like C++) will need to do so to have uniform defined behaviour
    44. Conclusion
      • Before diving into threads stop and think
        • Are threads really necessary?
        • Is the added complexity of threads going to reduce the complexity of the problem?
        • Would separate processes be better?
      • Proceed with care
        • Protect shared data
        • Avoid circular dependencies on mutexes
        • Use the correct synchronization method for the problem
    45. References
      • http://foldoc.org
      • http://www.cs.umass.edu/~mcorner/courses/691J/papers/TS/coffman_deadlocks/coffman_deadlocks.pdf
      • http://research.microsoft.com/~mbj/Mars_Pathfinder/Authoritative_Account.html

    + Tim PenheyTim Penhey, 9 months ago

    custom

    362 views, 0 favs, 0 embeds more stats

    An introduction to multithreading and why it is har more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 362
      • 362 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories