Concurrency and More
Concurrency and More
• During any given run of this program, the output might be “yes no” or “no yes”.
• Concurrent programs are hard to debug.
Shared Variables
• Most variables are local meaning that they belong to a single thread and no other
threads can access them.
• Shared among two or more threads; this is one of the ways threads interact with each
other. For example, one way to communicate information between threads is for one
thread to read a value written by another thread.
• Other ways that threads interact are concurrent writes (two or more writers)
• Concurrent updates (two or more threads performing a read followed by a write).
Concurrent writes
• Execution1: A1<B1<B2<A2
• Execution2: A1<A2<B1<B2
Semaphores
• Semaphore is a data structure that is useful for solving a variety of synchronization problems.
• A semaphore is like an integer, with three differences:
1. When you create the semaphore, you can initialize its value to any integer, but after that the
only operations you are allowed to perform are increment (increase by one) and decrement
(decrease by one). You cannot read the current value of the semaphore.
2. When a thread decrements the semaphore, if the result is negative, the thread blocks itself
(notifies the scheduler that it cannot proceed.) and cannot continue until another thread
increments the semaphore.
3. When a thread increments the semaphore, if there are other threads waiting, one of the
waiting threads gets unblocked.
Semaphores
• What the value of the semaphore means.
• If the value is positive, then it represents the number of threads that can decrement
without blocking.
• If it is negative, then it represents the number of threads that have blocked and are
waiting.
• If the value is zero, it means there are no threads waiting, but if a thread tries to
decrement, it will block.
Semaphores
read = Semaphore(1);
read.increment(); or read.signal();
read.decrement(); read.wait();
Or
read.V();
read.P();
Semaphore Patterns (signaling)
Thread A Thread B
1. Statement a; 1. Sem.wait();
2. Sem.signal(); 2. Statement b;
Semaphore Patterns (Rendezvous)
Thread A Thread B
1. Statement a1; 1. Statement b1;
2. Statement a2; 2. Statement b2;
We want to guarantee that a1 happens before b2 and b1 happens
before a2.
Semaphore Patterns (Rendezvous)
Thread A Thread B
1. Statement a1; 1. Statement b1;
2. AdoneSema.signal(); 2. BdoneSema.signal();
3. BdoneSema.wait(); 3. AdoneSema.wait();
4. Statement a2; 4. Statement b2;
What is the difference between the two?
Mutex: Semaphore for enforcing mutual
exclusion
Thread A Thread B
count = count + 1; count = count + 1;
How do we solve this problem?
Mutex Solution
Thread A Thread B
mutex.wait(); mutex.wait();
count = count + 1; count = count + 1;
mutex.signal(); mutex.signal();
Multiplex: Allow Multiple Threads
Thread A Thread B Thread C
mutex.down(); mutex.down(); mutex.down();
count = count + 1; count = count + 1; count = count + 1;
mutex.up(); mutex.up(); mutex.up();
mutex.signal () release