Condition Variables and Producer/Consumer - PowerPoint PPT Presentation

About This Presentation
Title:

Condition Variables and Producer/Consumer

Description:

Condition Variables and Producer/Consumer CSE451 Andrew Whitaker Background Often, a thread needs to wait on another thread until some condition becomes true Example ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 10
Provided by: AndrewW64
Category:

less

Transcript and Presenter's Notes

Title: Condition Variables and Producer/Consumer


1
Condition Variablesand Producer/Consumer
  • CSE451
  • Andrew Whitaker

2
Background
  • Often, a thread needs to wait on another thread
    until some condition becomes true
  • Example producer/consumer problems
  • Some threads produce items
  • Other threads consume items
  • Consuming threads block until there is something
    to consume
  • Condition variables provide a rendezvous
    mechanism
  • Wait until some condition becomes true

3
A Simple Example
  • Thread 1
  • Wait a second
  • Increment a counter
  • Repeat
  • Thread 2
  • Wait for the counter to reach 10
  • (in other words, wait ten seconds)

4
  1. public class TimerCounter implements Runnable
  2. // this counter gets incremented every second
  3. static volatile int count 0
  4. static final int TARGET 10
  5. public static void main(String args)
  6. // start the counter thread
  7. Thread t new Thread (new TimerCounter())
  8. t.start()
  9. // wait TARGET seconds
  10. while (count lt TARGET) // busy wait
  11. System.out.println(TARGET " seconds has
    expired")
  12. public void run()
  13. while (count lt TARGET)
  14. try

5
Revised Version
  • Replace busy waiting with a condition variable
  • Allows the thread to safely give up the processor
  • Essentially, we are replacing polling with
    interruption

6
  1. // class TimerCounter
  2. static final Object lockObject new Object()
  3. public static void main(String args)
  4. // initialization as before
  5. synchronized (lockObject)
  6. while (count lt TARGET)
  7. try
  8. lockObject.wait()
  9. catch (InterruptedException excp)
    // ignored
  10. System.out.println(TARGET " seconds has
    expired")
  11. public void run()
  12. while (count lt TARGET)
  13. try Thread.sleep(1000) // sleep
    for one second

7
Condition Variable Details
  • Condition variables support three operations
    wait,notify,notifyAll
  • Every condition variable is associated with a
    lock
  • The lock must be held when doing any operation
  • Invoking wait releases the lock
  • Lock is held when wait returns
  • Condition predicate should always be tested in a
    while loop

8
Under the Covers
  • Each condition variable has an associated wait
    queue
  • Lock ensures thread-safe access to this queue
  • notify Move one thread from blocked to runnable
  • notifyAll Move all threads from blocked to
    runnable

blocked threads
9
Example Bounded Buffer
  • Bounded buffer is a finite queue of elements
  • Commonly used to coordinate producers and
    consumers
  • Put adds an element to the queue
  • And, blocks if the queue is full
  • Take removes an element from the queue
  • And, blocks if the queue is empty

put
take
Write a Comment
User Comments (0)
About PowerShow.com