Concurrency Race conditions Atomic blocks Real-life mechanisms
Introducing Shared-Memory Concurrency
Race Conditions and Atomic Blocks Laura Effinger-Dean November 19, 2007
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Introducing Shared-Memory Concurrency Race Conditions and Atomic - - PowerPoint PPT Presentation
Concurrency Race conditions Atomic blocks Real-life mechanisms Introducing Shared-Memory Concurrency Race Conditions and Atomic Blocks Laura Effinger-Dean November 19, 2007 Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Why use concurrency? Communicating between threads Concurrency in Java/C
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Why use concurrency? Communicating between threads Concurrency in Java/C
◮ Example: web browser. Each “tab” becomes a separate thread. ◮ Example: Fairness – one slow computation only takes some of
◮ Run other threads while one is reading/writing to disk (or
◮ Use more than one CPU at the same time ◮ The way computers will get faster over the next 10 years ◮ So no parallelism means no faster. Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Why use concurrency? Communicating between threads Concurrency in Java/C
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Why use concurrency? Communicating between threads Concurrency in Java/C
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms What are race conditions? Example of race conditions
◮ Calling sleep doesn’t guarantee that the race condition will
◮ In general, programs are large and we don’t know where to
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
◮ E.g., in TwoThreads, we got into trouble because the updates
◮ Lots of variations on terminology: critical sections,
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
◮ Atomic blocks that are “too long” could cause the program to
◮ Atomic blocks that are “too short” might miss race conditions. Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Using atomic blocks to avoid race conditions Example: BankAccount.java Example: ProducerConsumer.java
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Locks
◮ Active area of research—maybe it will be integrated into Java
◮ But misuse of locks will violate the “all-at-once” policy. ◮ Or lead to other bugs we haven’t seen yet. Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms Locks
◮ Many threads might be waiting; one will “win.” ◮ The lock-implementor avoids race conditions on lock-acquire.
◮ Easy to mess up (e.g., use two different locks to protect the
◮ Deadlock: threads might get stuck forever waiting for locks
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms
Laura Effinger-Dean Introducing Shared-Memory Concurrency
Concurrency Race conditions Atomic blocks Real-life mechanisms
Laura Effinger-Dean Introducing Shared-Memory Concurrency