SlideShare a Scribd company logo
Compiled ver 1.0

Dr. C.V. Suresh Babu

5/12/2013

Revision ver 1.1
Revision ver 1.2
Revision ver 1.3
Revision ver 1.4
Revision ver 1.5
Edited ver 2.0

Dr. C.V. Suresh Babu

UNIT IV SHARED OBJECTS AND CONCURRENT OBJECTS 9
4.1. Shared Objects and Synchronization –
4.2. Properties of Mutual Exclusion4.2.1. The Moral –
4.2.2. The Producer–Consumer Problem –
4.2.3. The Readers–Writers Problem4.3. Realities of Parallelization4.3.1. Parallel Programming- Principles4.4. Mutual Exclusion4.4.1. Time4.4.2. Critical Sections—
4.5. Thread Solutions4.5.1. The Filter Lock4.5.2. Fairness4.5.2. Lamport’s Bakery Algorithm4.6. Bounded Timestamps4.6.1. Lower Bounds on the Number of Locations4.7. Concurrent Objects4.7.1. Concurrency and Correctness4.7.2. Sequential Objects4.7.3. Quiescent Consistency4.7.4. Sequential Consistency4.8. Linearizability4.8.1. Formal Definitions4.8.2. Progress Conditions4.9. The Java Memory Model
4.1. Shared Objects and Synchronization
When processes interact with one another two fundamental requirements must be satisfied:
 Synchronization
 Communication

Balancing the work load using a shared counter. Each thread gets a dynamically
determined number of numbers to test
Counter counter = new Counter(1); // shared by all threads
void primePrint {
long i = 0;
long limit = power(10, 10);
while (i< limit) { // loop until all numbers taken
i = counter.getAndIncrement(); // take next untaken number
if (isPrime(i))
print(i);
}
}
An implementation of the shared counter
public class Counter {
private long value; // counter starts at one
public Counter(inti) { // constructor initializes counter
value = i;
}
public long getAndIncrement() { // increment, returning prior value
return value++;
}
}
4.2. Properties of Mutual Exclusion
First, if one pet wants to enter the yard, then it eventually succeeds. Second, if both pets want
to enter the yard, then eventually at least one of them succeeds. We consider this deadlockfreedom property to be essential.
Another property of compelling interest is starvation-freedom (sometimes called lockoutfreedom): if a pet wants to enter the yard, will it eventually succeed?
Here, Alice and Bob‟s protocol performs poorly.
Need for Mutual Exclusion
 If there is no controlled access to shared data, processes or threads may get an
inconsistent view of this data
 The result of concurrent execution will depend on the order in which instructions are
interleaved.
 Errors are timing dependent and usually not reproducible.
 Assume P1 and P2 are executing this code and share the variable a
 Processes can be preempted at any time.
 Assume P1 is preempted after the input statement, and P2 then executes entirely
 The character echoed by P1 will be the one read by P2 !!
4.2.1. The Moral
First, we examine why shouting across the yard and placing cell phone calls did not work.
Two kinds of communication occur naturally in concurrent systems:
Transient communication requires both parties to participate at the same time.
Shouting, gestures, or cell phone calls are examples of transient communication.
Persistent communication allows the sender and receiver to participate at different
times. Posting letters, sending email, or leaving notes under rocks are all examples of
persistent communication.
2.2. PRODUCER-CONSUMER
The producer – consumer problem is a common implementation pattern for cooperating
processes or threads. Put simply, a producer produces information that is later consumed
by a consumer. Traditionally, the problem is defined as follows:
To allow the producer and consumer to run concurrently, the producer and
consumer must share a common buffer.
So that the consumer does not try to consume an item that has not yet been
produced, the two processes must be synchronized.
If the common data buffer is bounded, the consumer process must wait if the
buffer is empty, and the producer process must wait if the buffer is full.
There are many examples of this problem in “real world” applications.
e.g., a build system may run multiple processes concurrently. The compiler
process will produce assembly code to be read by the assembler process.
_
You would have had to worry about this problem in PA1 had you not used a
socket as the data buffer between your client and server processes. Essentially, the
operating system handled your synchronization issues
PRODUCER PROBLEM
Add last items to the queue. Set queue->empty = 0
i == WORK_MAX, producer exits.
CONSUMER PROBLEM
Remove an item from the queue. A check to see if the queue is empty shows that it is.
Set queue->empty = 1.
Set queue->full = 0
Start spinning.
4.2.3. Readers and Writers Problem

W
R

R
R

Motivation: Consider a shared database
– Two classes of users:
» Readers – never modify database
» Writers – read and modify database
– Is using a single lock on the whole database sufficient?
» Like to have many readers at the same time
» Only one writer at a time
Basic structure of a solution:
– Reader()
Wait until no writers
Access data base
Check out – wake up a waiting writer
– Writer()
Wait until no active readers or writers
Access database
Check out – wake up waiting readers or writer
–

State variables (Protected by a lock called “lock”):
» int AR: Number of active readers; initially = 0
» int WR: Number of waiting readers; initially = 0
» int AW: Number of active writers; initially = 0
» int WW: Number of waiting writers; initially = 0
» Condition okToRead = NIL
» ConditioinokToWrite = NIL
Transform an algorithm to a Java program
The dining philosopher‟s problem is useful for modelling processes that are competing for
exclusive access to a limited number of resources. Another famous problem is the readers
and writers problem, which models access to a data base: it is acceptable to have multiple
processes reading the data base at the same time, but if one process is writing the data
base, no other process may have access to the data base, not even readers.
A solution for this problem, as C fragment is shown below.

typedefint semaphore;

// use your imagination

semaphoremutex = 1;

// controls access to „rc‟

semaphore db = 1;

// controls access to the data base

intrc = 0;

// # of processes reading or wanting to

reader() {
while (true) {

// repeat forever

down(&mutex);

// get exclusive access to „rc‟

rc++;

// one reader more now

if (rc == 1) down(&db); // if this is the first reader …
read_data_base();

// access the data

rc--;

// one reader fewer now

up(&mutex);

// release exclusive access to „rc‟

use_data_read();

// noncritical section

}
}
writer() {
while (true) {

// repeat forever

think_up_data();
down(&db);

// get exclusive access

write_data_base()();

// update the data

up(&db);
}

// noncritical section

// releases exclusive access
}

PRODUCERS SIMPLE APPLICATION
producer()
{
while(true)
{
ifi == N //full buffer
turn = 1
while turn <> 0
{
// nothing
}
produceitem(&item)//produce the item
insertitem(item, buffer)//insert the item in the buffer
turn = 1
//process zone
}
}
consumer()
{
while(true)
{
ifi == 0 //empty buffer
turn = 0
while turn <> 1
{
// nothing
}
consumeitem(&item)
deleteitem(buffer)//erase the item from buffer
turn = 0
//process zone
}
}
Readers and Writers Application
A message distribution system is an example of Readers and Writers. We must keep track of
how many messages are in the queue. If one notebook exists where writers may write
information to, only one writer may write at a time. Confusion may arise if a reader is trying
read at the same as a writer is writing. Since readers only look at the data, but do not modify
the data, we can allow more than one reader to read at the same time. The main complexity
with this problems stems from allowing more than one reader to access the data at the same
time.
Writers have mutual exclusion, but multiple readers at the same time is allowed.
Here is a basic Python class that implements a solution to Readers and Writers using simple
locks:
Importthreading
classReader_writer(Object):
def__init__(self):
self.readers=0
self.writers=0
self.mutex1=threading.Lock()
self.mutex2=threading.Lock()
self.readPending=threading.Lock()
self.writeBlock=threading.Lock()
self.readBlock=threading.Lock()
# This is kind of complicated locking stuff. Don't worry about why there
# are so many locks, it is just part of the known solution to
# the readers and writers problem.
#
# The basic idea of the readers and writers algorithm is to use locks to
# quickly see how many other readers and writers there are. write Block
# is the only lock held while reading the data. Thus the reader only
# prevents writers from entering the critical section. Both write Block
# and read Block are held while writing to the data. Thus the writer
# blocks readers and other writers.
defreader(self,lastread):
"Reader of readers and writers algorithm"
self.readPending.acquire()
self.readBlock.acquire()
self.mutex1.acquire()
self.readers=self.readers+1
ifself.readers==1:self.writeBlock.acquire()
self.mutex1.release()
self.readBlock.release()
self.readPending.release()
#-------------------------#
# critical section
#
#-------------------------self.mutex1.acquire()
self.readers=self.readers-1
ifself.readers==0:self.writeBlock.release()
self.mutex1.release()
returnretVal
defwriter(self,data):
"Writer of readers and writers algorithm"
self.mutex2.acquire()
self.writers=self.writers+1
ifself.writers==1:self.readBlock.acquire()
self.mutex2.release()
self.writeBlock.acquire()
#-------------------------#
# critical section
#
#-------------------------self.writeBlock.release()
self.mutex2.acquire()
self.writers=self.writers-1
ifself.writers==0:self.readBlock.release()
self.mutex2.release()
4.4.2. Critical Sections
For brevity, we say a thread acquires (alternately locks) a lockwhen it executes alock()
method call, and releases (alternately unlocks) the lock when it executes an
1 class Counter {
2 private intvalue;
3 public Counter(intc) { // constructor
4 value = c;
5}
6 // increment and return prior value
7 public intgetAndIncrement() {
8 inttemp = value; // start of danger zone
9 value = temp + 1; // end of danger zone
10 return temp;
11 }
12 }
Figure 2.1 The Counter class.
2.2 Critical Sections 23
1 public interface Lock {
2 public void lock(); // before entering critical section
3 public void unlock(); // before leaving critical section
4}
Figure 2.2 The Lock interface.
1 public class Counter {
2 private long value;
3 private Lock lock; // to protect critical section
45
public long getAndIncrement() {
6 lock.lock(); // enter critical section
7 try {
8 long temp = value; // in critical section
9 value = temp + 1; // in critical section
10 }finally{
11 lock.unlock(); // leave critical section
12 }
13 return temp;
14 }
15 }
Figure 2.3 Using a lock object
.
Threads using the lock() and unlock()methods must follow a specific format. A thread is well
formedif:
1. each critical section is associated with a unique Lock object,
2. the thread calls that object‟s lock() method when it is trying to enter the
critical section, and
3. the thread calls the unlock() method when it leaves the critical section.
Pragma2.2.1. In Java, these methods should be used in the following structured way.
1 mutex.lock();
2 try {
3 ... // body
4 }finally{
5 mutex.unlock();
6}
This idiom ensures that the lock is acquired before entering the try block, andthat the lock is
released when control leaves the block, even if some statementin the block throws an
unexpected exception.
Mutual Exclusion Critical sections of different threads do not overlap. ForThreadsA and B,
and integers j and k, either CSk
A →CSj
B or CSj
B →CSk
A.
Freedom from Deadlock If some thread attempts to acquire the lock, then some thread will
succeed in acquiring the lock. If thread A calls lock() but never acquires the lock, then other
threads must be completing an infinite number of critical sections.
Freedom from Starvation Every thread that attempts to acquire the lock eventually
succeeds. Every call to lock() eventually returns. This property is sometimes called lockout
freedom.
4.5. Thread Solutions
The LockOneClass
The LockOnealgorithm satisfies mutual exclusion.
Proof: Suppose not. Then there exist integers j and k such that CSj
A _→ CSk
B and
CSk
B _→ CSj
A. Consider each thread‟s last execution of the lock() method before
entering its k-th (j-th) critical section.
Inspecting the code, we see that
writeA(flag[A] = true)→readA(flag[B] == false)→CSA (2.3.1)
writeB(flag[B] = true)→readB(flag[A] == false)→CSB (2.3.2)
readA(flag[B] == false)→writeB(flag[B] = true) (2.3.3)
Note that once flag[B] is set to true it remains true. It follows that Eq. 2.3.3
holds, since otherwise thread A could not have read flag[B] as false. Eq. 2.3.4
follows from Eqs. 2.3.1–2.3.3, and the transitivity of the precedence order.
class LockOneimplementsLock {
private boolean[] flag = new boolean[2];
// thread-local index, 0 or 1
public void lock() {
inti = ThreadID.get();
intj = 1 - i;
flag[i] = true;
while (flag[j]) {} // wait
}
public void unlock() {
inti = ThreadID.get();
flag[i] = false;
}
}
Figure 2.4 TheLockOnealgorithm.
writeA(flag[A] = true)→readA(flag[B] == false)→ (2.3.4)
writeB(flag[B] = true)→readB(flag[A] == false)
It follows that writeA(flag[A] = true)→readB(flag[A] == false) without an
intervening write to the flag[] array, a contradiction. _
The Lock One algorithm is inadequate because it deadlocks if thread executions
are interleaved. If writeA(flag[A] = true) and writeB(flag[B] = true) events
occur before readA(flag[B]) and readB(flag[A]) events, then both threads wait
forever. Nevertheless, LockOnehas an interesting property: if one thread runs
before the other, no deadlock occurs, and all is well.
The LockTwoClass
The LockTwoalgorithm satisfies mutual exclusion.
Proof: Suppose not. Then there exist integers j and k such that CSj
A _→ CSk
B and
CSk
B _→ CSj
A. Consider as before each thread‟s last execution of the lock() method
before entering its k-th (j-th) critical section.
Inspecting the code, we see that
writeA(victim = A)→readA(victim == B)→CSA (2.3.5)
writeB(victim = B)→readB(victim == A)→CSB (2.3.6)
Thread B must assign B to the victim field between events writeA(victim = A)
andreadA(victim = B) (see Eq. 2.3.5). Since this assignment is the last, we have
writeA(victim = A)→writeB(victim = B)→readA(victim == B). (2.3.7)
Once the victim field is set to B, it does not change, so any subsequent read
returnsB, contradicting Eq. 2.3.6. _
1 class LockTwoimplementsLock {
2 private volatile intvictim;
3 public void lock() {
4 inti = ThreadID.get();
5 victim = i; // let the other go first
6 while (victim == i) {} // wait
7}
8 public void unlock() {}
9}
Figure 2.5 TheLockTwoalgorithm.
Example of lock algorithm
The Peterson lock algorithm satisfies mutual exclusion.
Proof: Suppose not. As before, consider the last executions of the lock() method
by threads A and B. Inspecting the code, we see that
writeA(flag[A] = true)→ (2.3.8)
writeA(victim = A)→readA(flag[B])→readA(victim)→CSA
writeB(flag[B] = true)→ (2.3.9)
writeB(victim = B)→readB(flag[A])→readB(victim)→CSB
Assume, without loss of generality, that A was the last thread to write to the
victimfield.
writeB(victim = B)→writeA(victim = A) (2.3.10)
1 class Peterson implements Lock {
2 // thread-local index, 0 or 1
3 private volatile boolean[] flag = new boolean[2];
4 private volatile intvictim;
5 public void lock() {
6 inti = ThreadID.get();
7 intj = 1 - i;
8 flag[i] = true; // I’m interested
9 victim = i; // you go first
10 while (flag[j] && victim == i) {}; // wait
11 }
12 public void unlock() {
13 inti = ThreadID.get();
14 flag[i] = false; // I’m not interested
15 }
16 }
Figure 2.6 The Peterson lock algorithm.
4.5.1. The Filter Lock
The levels are depicted in Fig. 2.8. Levels satisfy two important properties:
_ At least one thread trying to enter level _ succeeds.
_ If more than one thread is trying to enter level _, then at least one is blocked
(i.e., continues to wait at that level).
2.4 The Filter Lock 29
1 class Filter implements Lock {
2 int[] level;
3 int[] victim;
4 public Filter(intn) {
5 level = new int[n];
6 victim = new int[n]; // use 1..n-1
7 for (inti = 0; i< n; i++) {
8 level[i] = 0;
9}
10 }
11 public void lock() {
12 intme = ThreadID.get();
13 for (inti = 1; i< n; i++) { //attempt level 1
14 level[me] = i;
15 victim[i] = me;
16 // spin while conflicts exist
17 while ((∃k != me) (level[k] >= i&& victim[i] == me)) {};
18 }
19 }
20 public void unlock() {
21 intme = ThreadID.get();
22 level[me] = 0;
23 }
24 }
Figure 2.7 The Filter lock algorithm.
4.5.2. Fairness
A doorwaysection, whose execution intervalDAconsists of a bounded numberof steps, and
2. awaitingsection, whose execution intervalWAmay take an unbounded numberof steps
4.5.2. Lamport’s Bakery Algorithm
1 class Bakery implements Lock {
2 boolean[] flag;
3 Label[] label;
4 public Bakery (intn) {
5 flag = new boolean[n];
6 label = new Label[n];
7 for (inti = 0; i< n; i++) {
8 flag[i] = false; label[i] = 0;
9}
10 }
11 public void lock() {
12 inti = ThreadID.get();
13 flag[i] = true;
14 label[i] = max(label[0], ...,label[n-1]) + 1;
15 while ((∃k != i)(flag[k] && (label[k],k) << (label[i],i))) {};
16 }
17 public void unlock() {
18 flag[ThreadID.get()] = false;
19 }
20 }
Figure 2.9 The Bakery lock algorithm.
4.6. Bounded Timestamps
we see that a thread needs two abilities:
_ to read the other threads‟ timestamps (scan), and
_ to assign itself a later timestamp (label).
1 public interface Timestamp {
2 booleancompare(Timestamp);
3}
4 public interface TimestampSystem {
5 public Timestamp[] scan();
6 public void label(Timestamp timestamp, inti);
7}
Figure 2.10 Atimestamping system interface.
4.6.1. Lower Bounds on the Number of Locations
Definition: A Lock object state sis inconsistent in any global state where some thread is in
the critical section, but the lock state is compatible with a global state in which no thread is in
the critical section or is trying to enter the critical section.
Lemma2.8.1. No deadlock-free Lock algorithm can enter an inconsistent state.
Proof: Suppose the Lock object is in an inconsistent state s, where no thread is in the critical
section or trying to enter. If thread B tries to enter the critical section, it must eventually
succeed, because the algorithm is deadlock-free. Suppose the Lock object is in an
inconsistent state s, where A is in the critical section. If thread B tries to enter the critical
section, it must block until A leaves.
We have a contradiction, because B cannot determine whether A is in the critical
section. _
Any Lock algorithm that solves deadlock-free mutual exclusion must have
Ndistinct locations. Here, we consider only the 3-thread case.Ndeadlock-freeLock algorithm
accessed by three threads must use three distinctlocations.
Definition 2.8.2.A covering state for a Lock object is one in which there is at leastone thread
about to write to each shared location, but the Lock object‟s locations“look” like the critical
section is empty (i.e., the locations‟ states appear as if thereis no thread either in the critical
section or trying to enter the critical section).
4.7.1. Concurrency and Correctness
Concurrent Objects
1 class LockBasedQueue<T> {
2 inthead, tail;
3 T[] items;
4 Lock lock;
5 public LockBasedQueue(intcapacity) {
6 head = 0; tail = 0;
7 lock = new ReentrantLock();
8 items = (T[])new Object[capacity];
9}
10 public void enq(T x) throws FullException {
11 lock.lock();
12 try {
13 if (tail - head == items.length)
14 throw new FullException();
15 items[tail % items.length] = x;
16 tail++;
17 }finally{
18 lock.unlock();
19 }
20 }
21 public T deq() throws EmptyException {
22 lock.lock();
23 try {
24 if (tail == head)
25 throw new EmptyException();
26 T x = items[head % items.length];
27 head++;
28 return x;
29 }finally{
30 lock.unlock();
31 }
32 }
33 }
Figure 3.1 A lock-based FIFO queue. The queue‟s items are kept in an array items, where
headis the index of the next item to dequeue, and tail is the index of the first open array
slot (modulo the capacity). The lock field is a lock that ensures that methods are mutually
exclusive. Initially head and tail are zero, and the queue is empty. If enq() finds the queue
is full, i.e., head and tail differ by the queue size, then it throws an exception. Otherwise,
there is room, so enq() stores the item at array entry tail, and then increments tail. The
deq() method works in a symmetric way.
A single-enqueuer/single-dequeuer FIFO queue.
1 class WaitFreeQueue<T> {
2 volatile inthead = 0, tail = 0;
3 T[] items;
4 public WaitFreeQueue(intcapacity) {
5 items = (T[])new Object[capacity];
6 head = 0; tail = 0;
7}
8 public void enq(T x) throws FullException {
9 if (tail - head == items.length)
10 throw new FullException();
11 items[tail % items.length] = x;
12 tail++;
13 }
14 public T deq() throws EmptyException {
15 if (tail - head == 0)
16 throw new EmptyException();
17 T x = items[head % items.length];
18 head++;
19 return x;
20 }
21 }

4.7.4. Sequential Consistency
An execution is sequentially consistent if the method calls can be correctly arranged
retaining the mutual order of method calls in eachthread.The calls in different threads can be
re-arranged however you wish, regardless of when they start and when they end.
An example of a sequentially consistent system is a naive notion of RAM in multi-core
processor. When different threads read from the memory, they actually read from a cache or a
register, which is synchronized with a common RAM at writes. While values read and written
on different cores may not be immediately synchronized (cores may diverge in the notion of
what value a cell contains), the values read and written on the same core are always
consistent with each other. This is naive, though: even this guarantee is relaxed by modern
compilers, and accesses to different variables within one thread can be reordered.
You might notice that the definition of sequential consistency does not directly prevents
methods to return values from the future (i.e. to read what will be written later in a different
thread). Yet I claim that RAM is sequentially consistent; how's that? The thing is that each
execution should be sequentially consistent. If read and write do not overlap, and write
follows the read, we may just stop the program between them. Since each execution should
be consistent, the value read should be written before in some thread. Therefore, this "future
prediction" is not possible in consistent data structures (not only those that are "sequentially"
consistent) even that the definition doesn't directly prevent it.
However, if we combine two sequentially consistent executions, the result won't be
sequentially consistent. But there is another model, weaker than linearizability, that makes it
possible.
4.7.3. Quiescent Consistency
An execution is quiescently consistent if the method calls can be correctly arranged
retaining the mutual order of calls separated by quiescence, a period of time where no method
is being called in any thread.
This consistency condition is stronger than sequential consistency, but is still not strong
enough that we usually expect from a multithreaded system. The reason to use this model is
to attain a higher degree of concurrency and better response time in return of weakening
consistency a bit.
Quiescent consistency is not as esoteric as you might think, especially since you barely
heard about it. Here's an easy way to imagine a quiescently consistent system: assume that all
writes are cached, and the cache is flushed to a persistent storage when there are no ongoing
writes (or after it reaches certain size). There are specialdatastructures that are quiescently
consistent.Quiescent consistency components can be combined to form quiescently consistent
components again. Note also, that strong consistency implies quiescent consistency, hence
you can combine these two kinds of systems, and the result will be quiescently consistent.
If you're a system developer, you should note that you are not bound to choose between
linearizability and "thread-unsafely" when designing a library. The models presented above
may be viewed as patterns that enumerate what behaviors in multithreaded context are
considered universally useful. Just consider carefully what your users need, and do not forget
to specify which model your system follows. It will also help to link to something that
explains what lies behind the words "something consistent".
4.8. LINEARIZABILITY
A concurrent object is a data object shared by concurrent processes. Linearizability is a
correctness condition for concurrent objects that exploits the semantics of abstract data types.
It permits a high degree of concurrency, yet it permits programmers to specify and reason
about concurrent objects using known techniques from the sequential domain. Linearizability
provides the illusion that each operation applied by concurrent processes takes effect
instantaneously at some point between its invocation and its response, implying that the
meaning of a concurrent object‟s operations can be given by pre- and post-conditions.
4.9. JAVA MEMORY MODEL:
The Java memory model describes how threads in the Java programming language interact
through memory. Together with the description of single-threaded execution of code,
the memory model provides the semantics of the Java programming language.
The Java programming language and platform provide thread capabilities. Synchronization
between threads is notoriously difficult for developers; this difficulty is compounded because
Java applications can run on a wide range of processors and operating systems. To be able to
draw conclusions about a program's behavior, Java's designers decided they had to clearly
define possible behaviors of all Java programs.
On modern platforms, code is frequently not executed in the order it was written. It is
reordered by the compiler, the processor and the memory subsystem to achieve maximum
performance. On multiprocessor architectures, individual processors may have their own
local caches that are out of sync with main memory. It is generally undesirable to require
threads to remain perfectly in sync with one another because this would be too costly from a
performance point of view. This means that at any given time, different threads may see
different values for the same shared data.
In a single-threaded environment, it is easy to reason about code execution. The typical
approach requires the system to implement as-if-serial semantics for individual threads in
isolation. When an individual thread executes, it will appear as if all of the actions taken by
that thread occur in the order they appear in the program, even if the actions themselves occur
out of order.
If one thread executes its instructions out of order, then another thread might see the fact that
those instructions were executed out of order, even if that did not affect the semantics of the
first thread. For example, consider two threads with the following instructions, executing
concurrently, where the variables x and y are both initialized to 0:
Thread 1 Thread 2
x = 1;
int r1 = y;
y = 2;
int r2 = x;
If no reorderings are performed, and the read of y in Thread 2 returns the value 2, then the
subsequent read of x should return the value 1, because the write to x was performed before
the write to y. However, if the two writes are reordered, then the read of y can return the
value 2, and the read of x can return the value 0.
The Java Memory Model (JMM) defines the allowable behavior of multithreaded programs,
and therefore describes when such reorderings are possible. It places execution-time
constraints on the relationship between threads and main memory in order to achieve
consistent and reliable Java applications. By doing this, it makes it possible to reason about
code execution in a multithreaded environment, even in the face of optimizations performed
by the dynamic compiler, the processor(s) and the caches.
For execution of a single thread, the rules are simple. The Java Language Specification
requires a Java Virtual Machine to observe within-thread as-if-serial semantics. The runtime
(which, in this case, usually refers to the dynamic compiler, the processor and the memory
subsystem) is free to introduce any useful execution optimizations as long as the result of the
thread in isolation is guaranteed to be exactly the same as it would have been had all the
statements been executed in the order the statements occurred in the program (also called
program order).
The major caveat of this is that as-if-serial semantics do not prevent different threads from
having different views of the data. The memory model provides clear guidance about what
values are allowed to be returned when the data are read. The basic rules imply that
individual actions can be reordered, as long as the as-if-serial semantics of the thread are not
violated, and actions that imply communication between threads, such as the acquisition or
release of a lock, ensure that actions that happen prior to them are seen by other threads that
see their effects. For example, everything that happens before the release of a lock will be
seen to be ordered before and visible to everything that happens after a subsequent
acquisition of that same lock.
Mathematically, there is a partial order called the happens-before order over all actions
performed by the program. The happens-before order subsumes the program order; if one
action occurs before another in the program order, it will occur before the other in the
happens-before order. In addition, releases and subsequent acquires of locks form edges in
the happens-before graph. A read is allowed to return the value of a write if that write is the
last write to that variable before the read along some path in the happens-before order, or if
the write is not ordered with respect to that read in the happens-before order.
The Java memory model was the first attempt to provide a comprehensive memory model for
a popular programming language.

More Related Content

What's hot

Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in Android
Ali Muzaffar
 
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Farwa Ansari
 
Implicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handlingImplicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handling
VIKASH MAINANWAL
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patterns
OktJona
 
Programming using MPI and OpenMP
Programming using MPI and OpenMPProgramming using MPI and OpenMP
Programming using MPI and OpenMP
Divya Tiwari
 
Lesson10 behavioral patterns
Lesson10 behavioral patternsLesson10 behavioral patterns
Lesson10 behavioral patterns
OktJona
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
Valerio Maggio
 
Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unit
gowher172236
 
How to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupHow to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check Tuneup
Bala Subra
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
OktJona
 
Critical Section in Operating System
Critical Section in Operating SystemCritical Section in Operating System
Critical Section in Operating System
MOHIT AGARWAL
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
guest38bf
 
Exception handling
Exception handlingException handling
Exception handling
Minal Maniar
 
Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Felix Chen
 
Integrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural LanguagesIntegrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural Languagesbutest
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
eShikshak
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
MOHIT DADU
 

What's hot (18)

Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in Android
 
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
 
Implicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handlingImplicit and explicit sequence control with exception handling
Implicit and explicit sequence control with exception handling
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patterns
 
Programming using MPI and OpenMP
Programming using MPI and OpenMPProgramming using MPI and OpenMP
Programming using MPI and OpenMP
 
Lesson10 behavioral patterns
Lesson10 behavioral patternsLesson10 behavioral patterns
Lesson10 behavioral patterns
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Notes of java first unit
Notes of java first unitNotes of java first unit
Notes of java first unit
 
How to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check TuneupHow to ace your .NET technical interview :: .Net Technical Check Tuneup
How to ace your .NET technical interview :: .Net Technical Check Tuneup
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
 
Critical Section in Operating System
Critical Section in Operating SystemCritical Section in Operating System
Critical Section in Operating System
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
Exception handling
Exception handlingException handling
Exception handling
 
SYNCHRONIZATION
SYNCHRONIZATIONSYNCHRONIZATION
SYNCHRONIZATION
 
Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)
 
Integrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural LanguagesIntegrating Model Checking and Procedural Languages
Integrating Model Checking and Procedural Languages
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
 

Viewers also liked

U2 pedagogy
U2 pedagogyU2 pedagogy
Tax DSS
Tax DSSTax DSS
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
Dr. C.V. Suresh Babu
 
Creating and editing a database
Creating and editing a databaseCreating and editing a database
Creating and editing a databasecrystalpullen
 
Oops Quiz
Oops QuizOops Quiz
U1 pedagogy
U1 pedagogyU1 pedagogy
Test planoutline
Test planoutlineTest planoutline
Test planoutline
Dr. C.V. Suresh Babu
 
Creating a database
Creating a databaseCreating a database
Creating a databaseRahul Gupta
 
Elements of a Successful Computer System ver 1.0
Elements of a Successful Computer System ver 1.0Elements of a Successful Computer System ver 1.0
Elements of a Successful Computer System ver 1.0
Dr. C.V. Suresh Babu
 
Ict u2
Ict u2Ict u2
Seminar
SeminarSeminar
ICT Unit 1
ICT Unit 1ICT Unit 1

Viewers also liked (20)

U2 pedagogy
U2 pedagogyU2 pedagogy
U2 pedagogy
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Uml introduciton
Uml introducitonUml introduciton
Uml introduciton
 
ERP Making it happen
ERP Making it happenERP Making it happen
ERP Making it happen
 
Viva faq
Viva faqViva faq
Viva faq
 
Sad quiz
Sad quizSad quiz
Sad quiz
 
ERP systems implementation
ERP systems implementationERP systems implementation
ERP systems implementation
 
Tax DSS
Tax DSSTax DSS
Tax DSS
 
Adsa u2 ver 1.0.
Adsa u2 ver 1.0.Adsa u2 ver 1.0.
Adsa u2 ver 1.0.
 
Creating and editing a database
Creating and editing a databaseCreating and editing a database
Creating and editing a database
 
Assesment process model
Assesment process modelAssesment process model
Assesment process model
 
Databases
DatabasesDatabases
Databases
 
Oops Quiz
Oops QuizOops Quiz
Oops Quiz
 
U1 pedagogy
U1 pedagogyU1 pedagogy
U1 pedagogy
 
Test planoutline
Test planoutlineTest planoutline
Test planoutline
 
Creating a database
Creating a databaseCreating a database
Creating a database
 
Elements of a Successful Computer System ver 1.0
Elements of a Successful Computer System ver 1.0Elements of a Successful Computer System ver 1.0
Elements of a Successful Computer System ver 1.0
 
Ict u2
Ict u2Ict u2
Ict u2
 
Seminar
SeminarSeminar
Seminar
 
ICT Unit 1
ICT Unit 1ICT Unit 1
ICT Unit 1
 

Similar to Adsa u4 ver 1.0

Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
lodhran-hayat
 
Top 5 performance problems in .net applications application performance mon...
Top 5 performance problems in .net applications   application performance mon...Top 5 performance problems in .net applications   application performance mon...
Top 5 performance problems in .net applications application performance mon...
KennaaTol
 
2014 CrossRef Workshops: Co-Access
2014 CrossRef Workshops: Co-Access 2014 CrossRef Workshops: Co-Access
2014 CrossRef Workshops: Co-Access
Crossref
 
2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire
Marko Mitranić
 
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESPARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
suthi
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Triage Presentation
Triage PresentationTriage Presentation
Triage Presentation
AashishBalaji1
 
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
Mohammad Hossein Karami
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Writing code for people
Writing code for peopleWriting code for people
Writing code for people
Alexey Ivanov
 
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Codemotion
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
MuhammadBilal187526
 
what every web and app developer should know about multithreading
what every web and app developer should know about multithreadingwhat every web and app developer should know about multithreading
what every web and app developer should know about multithreading
Ilya Haykinson
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
Heartin Jacob
 
Eventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introductionEventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introductionMarc Seeger
 
multithreading
multithreadingmultithreading
multithreading
Rajkattamuri
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
luccastera
 

Similar to Adsa u4 ver 1.0 (20)

Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Top 5 performance problems in .net applications application performance mon...
Top 5 performance problems in .net applications   application performance mon...Top 5 performance problems in .net applications   application performance mon...
Top 5 performance problems in .net applications application performance mon...
 
2014 CrossRef Workshops: Co-Access
2014 CrossRef Workshops: Co-Access 2014 CrossRef Workshops: Co-Access
2014 CrossRef Workshops: Co-Access
 
2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire
 
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESPARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Triage Presentation
Triage PresentationTriage Presentation
Triage Presentation
 
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Writing code for people
Writing code for peopleWriting code for people
Writing code for people
 
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
what every web and app developer should know about multithreading
what every web and app developer should know about multithreadingwhat every web and app developer should know about multithreading
what every web and app developer should know about multithreading
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Eventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introductionEventdriven I/O - A hands on introduction
Eventdriven I/O - A hands on introduction
 
multithreading
multithreadingmultithreading
multithreading
 
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple SpacesConcurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
 

More from Dr. C.V. Suresh Babu

Data analytics with R
Data analytics with RData analytics with R
Data analytics with R
Dr. C.V. Suresh Babu
 
Association rules
Association rulesAssociation rules
Association rules
Dr. C.V. Suresh Babu
 
Clustering
ClusteringClustering
Classification
ClassificationClassification
Classification
Dr. C.V. Suresh Babu
 
Blue property assumptions.
Blue property assumptions.Blue property assumptions.
Blue property assumptions.
Dr. C.V. Suresh Babu
 
Introduction to regression
Introduction to regressionIntroduction to regression
Introduction to regression
Dr. C.V. Suresh Babu
 
DART
DARTDART
Mycin
MycinMycin
Expert systems
Expert systemsExpert systems
Expert systems
Dr. C.V. Suresh Babu
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
Dr. C.V. Suresh Babu
 
Bayes network
Bayes networkBayes network
Bayes network
Dr. C.V. Suresh Babu
 
Bayes' theorem
Bayes' theoremBayes' theorem
Bayes' theorem
Dr. C.V. Suresh Babu
 
Knowledge based agents
Knowledge based agentsKnowledge based agents
Knowledge based agents
Dr. C.V. Suresh Babu
 
Rule based system
Rule based systemRule based system
Rule based system
Dr. C.V. Suresh Babu
 
Formal Logic in AI
Formal Logic in AIFormal Logic in AI
Formal Logic in AI
Dr. C.V. Suresh Babu
 
Production based system
Production based systemProduction based system
Production based system
Dr. C.V. Suresh Babu
 
Game playing in AI
Game playing in AIGame playing in AI
Game playing in AI
Dr. C.V. Suresh Babu
 
Diagnosis test of diabetics and hypertension by AI
Diagnosis test of diabetics and hypertension by AIDiagnosis test of diabetics and hypertension by AI
Diagnosis test of diabetics and hypertension by AI
Dr. C.V. Suresh Babu
 
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”
Dr. C.V. Suresh Babu
 
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”
Dr. C.V. Suresh Babu
 

More from Dr. C.V. Suresh Babu (20)

Data analytics with R
Data analytics with RData analytics with R
Data analytics with R
 
Association rules
Association rulesAssociation rules
Association rules
 
Clustering
ClusteringClustering
Clustering
 
Classification
ClassificationClassification
Classification
 
Blue property assumptions.
Blue property assumptions.Blue property assumptions.
Blue property assumptions.
 
Introduction to regression
Introduction to regressionIntroduction to regression
Introduction to regression
 
DART
DARTDART
DART
 
Mycin
MycinMycin
Mycin
 
Expert systems
Expert systemsExpert systems
Expert systems
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
 
Bayes network
Bayes networkBayes network
Bayes network
 
Bayes' theorem
Bayes' theoremBayes' theorem
Bayes' theorem
 
Knowledge based agents
Knowledge based agentsKnowledge based agents
Knowledge based agents
 
Rule based system
Rule based systemRule based system
Rule based system
 
Formal Logic in AI
Formal Logic in AIFormal Logic in AI
Formal Logic in AI
 
Production based system
Production based systemProduction based system
Production based system
 
Game playing in AI
Game playing in AIGame playing in AI
Game playing in AI
 
Diagnosis test of diabetics and hypertension by AI
Diagnosis test of diabetics and hypertension by AIDiagnosis test of diabetics and hypertension by AI
Diagnosis test of diabetics and hypertension by AI
 
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”
 
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”
 

Recently uploaded

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 

Recently uploaded (20)

Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 

Adsa u4 ver 1.0

  • 1. Compiled ver 1.0 Dr. C.V. Suresh Babu 5/12/2013 Revision ver 1.1 Revision ver 1.2 Revision ver 1.3 Revision ver 1.4 Revision ver 1.5 Edited ver 2.0 Dr. C.V. Suresh Babu UNIT IV SHARED OBJECTS AND CONCURRENT OBJECTS 9 4.1. Shared Objects and Synchronization – 4.2. Properties of Mutual Exclusion4.2.1. The Moral – 4.2.2. The Producer–Consumer Problem – 4.2.3. The Readers–Writers Problem4.3. Realities of Parallelization4.3.1. Parallel Programming- Principles4.4. Mutual Exclusion4.4.1. Time4.4.2. Critical Sections— 4.5. Thread Solutions4.5.1. The Filter Lock4.5.2. Fairness4.5.2. Lamport’s Bakery Algorithm4.6. Bounded Timestamps4.6.1. Lower Bounds on the Number of Locations4.7. Concurrent Objects4.7.1. Concurrency and Correctness4.7.2. Sequential Objects4.7.3. Quiescent Consistency4.7.4. Sequential Consistency4.8. Linearizability4.8.1. Formal Definitions4.8.2. Progress Conditions4.9. The Java Memory Model
  • 2. 4.1. Shared Objects and Synchronization When processes interact with one another two fundamental requirements must be satisfied:  Synchronization  Communication Balancing the work load using a shared counter. Each thread gets a dynamically determined number of numbers to test Counter counter = new Counter(1); // shared by all threads void primePrint { long i = 0; long limit = power(10, 10); while (i< limit) { // loop until all numbers taken i = counter.getAndIncrement(); // take next untaken number if (isPrime(i)) print(i); } }
  • 3. An implementation of the shared counter public class Counter { private long value; // counter starts at one public Counter(inti) { // constructor initializes counter value = i; } public long getAndIncrement() { // increment, returning prior value return value++; } } 4.2. Properties of Mutual Exclusion First, if one pet wants to enter the yard, then it eventually succeeds. Second, if both pets want to enter the yard, then eventually at least one of them succeeds. We consider this deadlockfreedom property to be essential. Another property of compelling interest is starvation-freedom (sometimes called lockoutfreedom): if a pet wants to enter the yard, will it eventually succeed? Here, Alice and Bob‟s protocol performs poorly. Need for Mutual Exclusion  If there is no controlled access to shared data, processes or threads may get an inconsistent view of this data  The result of concurrent execution will depend on the order in which instructions are interleaved.  Errors are timing dependent and usually not reproducible.  Assume P1 and P2 are executing this code and share the variable a  Processes can be preempted at any time.  Assume P1 is preempted after the input statement, and P2 then executes entirely  The character echoed by P1 will be the one read by P2 !!
  • 4. 4.2.1. The Moral First, we examine why shouting across the yard and placing cell phone calls did not work. Two kinds of communication occur naturally in concurrent systems: Transient communication requires both parties to participate at the same time. Shouting, gestures, or cell phone calls are examples of transient communication. Persistent communication allows the sender and receiver to participate at different times. Posting letters, sending email, or leaving notes under rocks are all examples of persistent communication. 2.2. PRODUCER-CONSUMER The producer – consumer problem is a common implementation pattern for cooperating processes or threads. Put simply, a producer produces information that is later consumed by a consumer. Traditionally, the problem is defined as follows: To allow the producer and consumer to run concurrently, the producer and consumer must share a common buffer. So that the consumer does not try to consume an item that has not yet been produced, the two processes must be synchronized. If the common data buffer is bounded, the consumer process must wait if the buffer is empty, and the producer process must wait if the buffer is full. There are many examples of this problem in “real world” applications.
  • 5. e.g., a build system may run multiple processes concurrently. The compiler process will produce assembly code to be read by the assembler process. _ You would have had to worry about this problem in PA1 had you not used a socket as the data buffer between your client and server processes. Essentially, the operating system handled your synchronization issues PRODUCER PROBLEM Add last items to the queue. Set queue->empty = 0 i == WORK_MAX, producer exits. CONSUMER PROBLEM Remove an item from the queue. A check to see if the queue is empty shows that it is. Set queue->empty = 1. Set queue->full = 0 Start spinning. 4.2.3. Readers and Writers Problem W R R R Motivation: Consider a shared database – Two classes of users: » Readers – never modify database » Writers – read and modify database – Is using a single lock on the whole database sufficient? » Like to have many readers at the same time » Only one writer at a time Basic structure of a solution: – Reader() Wait until no writers Access data base Check out – wake up a waiting writer – Writer() Wait until no active readers or writers Access database Check out – wake up waiting readers or writer
  • 6. – State variables (Protected by a lock called “lock”): » int AR: Number of active readers; initially = 0 » int WR: Number of waiting readers; initially = 0 » int AW: Number of active writers; initially = 0 » int WW: Number of waiting writers; initially = 0 » Condition okToRead = NIL » ConditioinokToWrite = NIL Transform an algorithm to a Java program The dining philosopher‟s problem is useful for modelling processes that are competing for exclusive access to a limited number of resources. Another famous problem is the readers and writers problem, which models access to a data base: it is acceptable to have multiple processes reading the data base at the same time, but if one process is writing the data base, no other process may have access to the data base, not even readers. A solution for this problem, as C fragment is shown below. typedefint semaphore; // use your imagination semaphoremutex = 1; // controls access to „rc‟ semaphore db = 1; // controls access to the data base intrc = 0; // # of processes reading or wanting to reader() { while (true) { // repeat forever down(&mutex); // get exclusive access to „rc‟ rc++; // one reader more now if (rc == 1) down(&db); // if this is the first reader … read_data_base(); // access the data rc--; // one reader fewer now up(&mutex); // release exclusive access to „rc‟ use_data_read(); // noncritical section } } writer() { while (true) { // repeat forever think_up_data(); down(&db); // get exclusive access write_data_base()(); // update the data up(&db); } // noncritical section // releases exclusive access
  • 7. } PRODUCERS SIMPLE APPLICATION producer() { while(true) { ifi == N //full buffer turn = 1 while turn <> 0 { // nothing } produceitem(&item)//produce the item insertitem(item, buffer)//insert the item in the buffer turn = 1 //process zone } } consumer() { while(true) { ifi == 0 //empty buffer turn = 0 while turn <> 1 { // nothing } consumeitem(&item) deleteitem(buffer)//erase the item from buffer turn = 0 //process zone } } Readers and Writers Application A message distribution system is an example of Readers and Writers. We must keep track of how many messages are in the queue. If one notebook exists where writers may write information to, only one writer may write at a time. Confusion may arise if a reader is trying read at the same as a writer is writing. Since readers only look at the data, but do not modify the data, we can allow more than one reader to read at the same time. The main complexity with this problems stems from allowing more than one reader to access the data at the same time.
  • 8. Writers have mutual exclusion, but multiple readers at the same time is allowed. Here is a basic Python class that implements a solution to Readers and Writers using simple locks: Importthreading classReader_writer(Object): def__init__(self): self.readers=0 self.writers=0 self.mutex1=threading.Lock() self.mutex2=threading.Lock() self.readPending=threading.Lock() self.writeBlock=threading.Lock() self.readBlock=threading.Lock() # This is kind of complicated locking stuff. Don't worry about why there # are so many locks, it is just part of the known solution to # the readers and writers problem. # # The basic idea of the readers and writers algorithm is to use locks to # quickly see how many other readers and writers there are. write Block # is the only lock held while reading the data. Thus the reader only # prevents writers from entering the critical section. Both write Block # and read Block are held while writing to the data. Thus the writer # blocks readers and other writers.
  • 9. defreader(self,lastread): "Reader of readers and writers algorithm" self.readPending.acquire() self.readBlock.acquire() self.mutex1.acquire() self.readers=self.readers+1 ifself.readers==1:self.writeBlock.acquire() self.mutex1.release() self.readBlock.release() self.readPending.release() #-------------------------# # critical section # #-------------------------self.mutex1.acquire() self.readers=self.readers-1 ifself.readers==0:self.writeBlock.release() self.mutex1.release() returnretVal defwriter(self,data): "Writer of readers and writers algorithm" self.mutex2.acquire() self.writers=self.writers+1 ifself.writers==1:self.readBlock.acquire() self.mutex2.release() self.writeBlock.acquire() #-------------------------# # critical section # #-------------------------self.writeBlock.release() self.mutex2.acquire() self.writers=self.writers-1 ifself.writers==0:self.readBlock.release() self.mutex2.release() 4.4.2. Critical Sections For brevity, we say a thread acquires (alternately locks) a lockwhen it executes alock() method call, and releases (alternately unlocks) the lock when it executes an 1 class Counter { 2 private intvalue; 3 public Counter(intc) { // constructor 4 value = c; 5} 6 // increment and return prior value 7 public intgetAndIncrement() {
  • 10. 8 inttemp = value; // start of danger zone 9 value = temp + 1; // end of danger zone 10 return temp; 11 } 12 } Figure 2.1 The Counter class. 2.2 Critical Sections 23 1 public interface Lock { 2 public void lock(); // before entering critical section 3 public void unlock(); // before leaving critical section 4} Figure 2.2 The Lock interface. 1 public class Counter { 2 private long value; 3 private Lock lock; // to protect critical section 45 public long getAndIncrement() { 6 lock.lock(); // enter critical section 7 try { 8 long temp = value; // in critical section 9 value = temp + 1; // in critical section 10 }finally{ 11 lock.unlock(); // leave critical section 12 } 13 return temp; 14 } 15 } Figure 2.3 Using a lock object . Threads using the lock() and unlock()methods must follow a specific format. A thread is well formedif: 1. each critical section is associated with a unique Lock object, 2. the thread calls that object‟s lock() method when it is trying to enter the critical section, and 3. the thread calls the unlock() method when it leaves the critical section. Pragma2.2.1. In Java, these methods should be used in the following structured way. 1 mutex.lock(); 2 try { 3 ... // body 4 }finally{ 5 mutex.unlock(); 6} This idiom ensures that the lock is acquired before entering the try block, andthat the lock is released when control leaves the block, even if some statementin the block throws an unexpected exception.
  • 11. Mutual Exclusion Critical sections of different threads do not overlap. ForThreadsA and B, and integers j and k, either CSk A →CSj B or CSj B →CSk A. Freedom from Deadlock If some thread attempts to acquire the lock, then some thread will succeed in acquiring the lock. If thread A calls lock() but never acquires the lock, then other threads must be completing an infinite number of critical sections. Freedom from Starvation Every thread that attempts to acquire the lock eventually succeeds. Every call to lock() eventually returns. This property is sometimes called lockout freedom. 4.5. Thread Solutions The LockOneClass The LockOnealgorithm satisfies mutual exclusion. Proof: Suppose not. Then there exist integers j and k such that CSj A _→ CSk B and CSk B _→ CSj A. Consider each thread‟s last execution of the lock() method before entering its k-th (j-th) critical section. Inspecting the code, we see that writeA(flag[A] = true)→readA(flag[B] == false)→CSA (2.3.1) writeB(flag[B] = true)→readB(flag[A] == false)→CSB (2.3.2) readA(flag[B] == false)→writeB(flag[B] = true) (2.3.3) Note that once flag[B] is set to true it remains true. It follows that Eq. 2.3.3 holds, since otherwise thread A could not have read flag[B] as false. Eq. 2.3.4 follows from Eqs. 2.3.1–2.3.3, and the transitivity of the precedence order. class LockOneimplementsLock { private boolean[] flag = new boolean[2]; // thread-local index, 0 or 1 public void lock() { inti = ThreadID.get(); intj = 1 - i; flag[i] = true; while (flag[j]) {} // wait } public void unlock() { inti = ThreadID.get(); flag[i] = false; } } Figure 2.4 TheLockOnealgorithm. writeA(flag[A] = true)→readA(flag[B] == false)→ (2.3.4) writeB(flag[B] = true)→readB(flag[A] == false) It follows that writeA(flag[A] = true)→readB(flag[A] == false) without an
  • 12. intervening write to the flag[] array, a contradiction. _ The Lock One algorithm is inadequate because it deadlocks if thread executions are interleaved. If writeA(flag[A] = true) and writeB(flag[B] = true) events occur before readA(flag[B]) and readB(flag[A]) events, then both threads wait forever. Nevertheless, LockOnehas an interesting property: if one thread runs before the other, no deadlock occurs, and all is well. The LockTwoClass The LockTwoalgorithm satisfies mutual exclusion. Proof: Suppose not. Then there exist integers j and k such that CSj A _→ CSk B and CSk B _→ CSj A. Consider as before each thread‟s last execution of the lock() method before entering its k-th (j-th) critical section. Inspecting the code, we see that writeA(victim = A)→readA(victim == B)→CSA (2.3.5) writeB(victim = B)→readB(victim == A)→CSB (2.3.6) Thread B must assign B to the victim field between events writeA(victim = A) andreadA(victim = B) (see Eq. 2.3.5). Since this assignment is the last, we have writeA(victim = A)→writeB(victim = B)→readA(victim == B). (2.3.7) Once the victim field is set to B, it does not change, so any subsequent read returnsB, contradicting Eq. 2.3.6. _ 1 class LockTwoimplementsLock { 2 private volatile intvictim; 3 public void lock() { 4 inti = ThreadID.get(); 5 victim = i; // let the other go first 6 while (victim == i) {} // wait 7} 8 public void unlock() {} 9} Figure 2.5 TheLockTwoalgorithm. Example of lock algorithm The Peterson lock algorithm satisfies mutual exclusion. Proof: Suppose not. As before, consider the last executions of the lock() method by threads A and B. Inspecting the code, we see that writeA(flag[A] = true)→ (2.3.8) writeA(victim = A)→readA(flag[B])→readA(victim)→CSA writeB(flag[B] = true)→ (2.3.9) writeB(victim = B)→readB(flag[A])→readB(victim)→CSB Assume, without loss of generality, that A was the last thread to write to the victimfield. writeB(victim = B)→writeA(victim = A) (2.3.10) 1 class Peterson implements Lock { 2 // thread-local index, 0 or 1 3 private volatile boolean[] flag = new boolean[2];
  • 13. 4 private volatile intvictim; 5 public void lock() { 6 inti = ThreadID.get(); 7 intj = 1 - i; 8 flag[i] = true; // I’m interested 9 victim = i; // you go first 10 while (flag[j] && victim == i) {}; // wait 11 } 12 public void unlock() { 13 inti = ThreadID.get(); 14 flag[i] = false; // I’m not interested 15 } 16 } Figure 2.6 The Peterson lock algorithm. 4.5.1. The Filter Lock The levels are depicted in Fig. 2.8. Levels satisfy two important properties: _ At least one thread trying to enter level _ succeeds. _ If more than one thread is trying to enter level _, then at least one is blocked (i.e., continues to wait at that level). 2.4 The Filter Lock 29 1 class Filter implements Lock { 2 int[] level; 3 int[] victim; 4 public Filter(intn) { 5 level = new int[n]; 6 victim = new int[n]; // use 1..n-1 7 for (inti = 0; i< n; i++) { 8 level[i] = 0; 9} 10 } 11 public void lock() { 12 intme = ThreadID.get(); 13 for (inti = 1; i< n; i++) { //attempt level 1 14 level[me] = i; 15 victim[i] = me; 16 // spin while conflicts exist 17 while ((∃k != me) (level[k] >= i&& victim[i] == me)) {}; 18 } 19 } 20 public void unlock() { 21 intme = ThreadID.get(); 22 level[me] = 0; 23 } 24 } Figure 2.7 The Filter lock algorithm. 4.5.2. Fairness A doorwaysection, whose execution intervalDAconsists of a bounded numberof steps, and 2. awaitingsection, whose execution intervalWAmay take an unbounded numberof steps
  • 14. 4.5.2. Lamport’s Bakery Algorithm 1 class Bakery implements Lock { 2 boolean[] flag; 3 Label[] label; 4 public Bakery (intn) { 5 flag = new boolean[n]; 6 label = new Label[n]; 7 for (inti = 0; i< n; i++) { 8 flag[i] = false; label[i] = 0; 9} 10 } 11 public void lock() { 12 inti = ThreadID.get(); 13 flag[i] = true; 14 label[i] = max(label[0], ...,label[n-1]) + 1; 15 while ((∃k != i)(flag[k] && (label[k],k) << (label[i],i))) {}; 16 } 17 public void unlock() { 18 flag[ThreadID.get()] = false; 19 } 20 } Figure 2.9 The Bakery lock algorithm. 4.6. Bounded Timestamps we see that a thread needs two abilities: _ to read the other threads‟ timestamps (scan), and _ to assign itself a later timestamp (label). 1 public interface Timestamp { 2 booleancompare(Timestamp); 3} 4 public interface TimestampSystem { 5 public Timestamp[] scan(); 6 public void label(Timestamp timestamp, inti); 7} Figure 2.10 Atimestamping system interface. 4.6.1. Lower Bounds on the Number of Locations Definition: A Lock object state sis inconsistent in any global state where some thread is in the critical section, but the lock state is compatible with a global state in which no thread is in the critical section or is trying to enter the critical section. Lemma2.8.1. No deadlock-free Lock algorithm can enter an inconsistent state. Proof: Suppose the Lock object is in an inconsistent state s, where no thread is in the critical section or trying to enter. If thread B tries to enter the critical section, it must eventually succeed, because the algorithm is deadlock-free. Suppose the Lock object is in an
  • 15. inconsistent state s, where A is in the critical section. If thread B tries to enter the critical section, it must block until A leaves. We have a contradiction, because B cannot determine whether A is in the critical section. _ Any Lock algorithm that solves deadlock-free mutual exclusion must have Ndistinct locations. Here, we consider only the 3-thread case.Ndeadlock-freeLock algorithm accessed by three threads must use three distinctlocations. Definition 2.8.2.A covering state for a Lock object is one in which there is at leastone thread about to write to each shared location, but the Lock object‟s locations“look” like the critical section is empty (i.e., the locations‟ states appear as if thereis no thread either in the critical section or trying to enter the critical section). 4.7.1. Concurrency and Correctness Concurrent Objects 1 class LockBasedQueue<T> { 2 inthead, tail; 3 T[] items; 4 Lock lock; 5 public LockBasedQueue(intcapacity) { 6 head = 0; tail = 0; 7 lock = new ReentrantLock(); 8 items = (T[])new Object[capacity]; 9} 10 public void enq(T x) throws FullException { 11 lock.lock(); 12 try { 13 if (tail - head == items.length) 14 throw new FullException(); 15 items[tail % items.length] = x; 16 tail++; 17 }finally{ 18 lock.unlock(); 19 } 20 } 21 public T deq() throws EmptyException { 22 lock.lock(); 23 try { 24 if (tail == head) 25 throw new EmptyException(); 26 T x = items[head % items.length]; 27 head++; 28 return x; 29 }finally{ 30 lock.unlock(); 31 } 32 } 33 } Figure 3.1 A lock-based FIFO queue. The queue‟s items are kept in an array items, where headis the index of the next item to dequeue, and tail is the index of the first open array
  • 16. slot (modulo the capacity). The lock field is a lock that ensures that methods are mutually exclusive. Initially head and tail are zero, and the queue is empty. If enq() finds the queue is full, i.e., head and tail differ by the queue size, then it throws an exception. Otherwise, there is room, so enq() stores the item at array entry tail, and then increments tail. The deq() method works in a symmetric way. A single-enqueuer/single-dequeuer FIFO queue. 1 class WaitFreeQueue<T> { 2 volatile inthead = 0, tail = 0; 3 T[] items; 4 public WaitFreeQueue(intcapacity) { 5 items = (T[])new Object[capacity]; 6 head = 0; tail = 0; 7} 8 public void enq(T x) throws FullException { 9 if (tail - head == items.length) 10 throw new FullException(); 11 items[tail % items.length] = x; 12 tail++; 13 } 14 public T deq() throws EmptyException { 15 if (tail - head == 0) 16 throw new EmptyException(); 17 T x = items[head % items.length]; 18 head++; 19 return x; 20 } 21 } 4.7.4. Sequential Consistency An execution is sequentially consistent if the method calls can be correctly arranged retaining the mutual order of method calls in eachthread.The calls in different threads can be re-arranged however you wish, regardless of when they start and when they end. An example of a sequentially consistent system is a naive notion of RAM in multi-core processor. When different threads read from the memory, they actually read from a cache or a register, which is synchronized with a common RAM at writes. While values read and written on different cores may not be immediately synchronized (cores may diverge in the notion of what value a cell contains), the values read and written on the same core are always consistent with each other. This is naive, though: even this guarantee is relaxed by modern compilers, and accesses to different variables within one thread can be reordered. You might notice that the definition of sequential consistency does not directly prevents methods to return values from the future (i.e. to read what will be written later in a different thread). Yet I claim that RAM is sequentially consistent; how's that? The thing is that each execution should be sequentially consistent. If read and write do not overlap, and write
  • 17. follows the read, we may just stop the program between them. Since each execution should be consistent, the value read should be written before in some thread. Therefore, this "future prediction" is not possible in consistent data structures (not only those that are "sequentially" consistent) even that the definition doesn't directly prevent it. However, if we combine two sequentially consistent executions, the result won't be sequentially consistent. But there is another model, weaker than linearizability, that makes it possible. 4.7.3. Quiescent Consistency An execution is quiescently consistent if the method calls can be correctly arranged retaining the mutual order of calls separated by quiescence, a period of time where no method is being called in any thread. This consistency condition is stronger than sequential consistency, but is still not strong enough that we usually expect from a multithreaded system. The reason to use this model is to attain a higher degree of concurrency and better response time in return of weakening consistency a bit. Quiescent consistency is not as esoteric as you might think, especially since you barely heard about it. Here's an easy way to imagine a quiescently consistent system: assume that all writes are cached, and the cache is flushed to a persistent storage when there are no ongoing writes (or after it reaches certain size). There are specialdatastructures that are quiescently consistent.Quiescent consistency components can be combined to form quiescently consistent components again. Note also, that strong consistency implies quiescent consistency, hence you can combine these two kinds of systems, and the result will be quiescently consistent. If you're a system developer, you should note that you are not bound to choose between linearizability and "thread-unsafely" when designing a library. The models presented above may be viewed as patterns that enumerate what behaviors in multithreaded context are considered universally useful. Just consider carefully what your users need, and do not forget to specify which model your system follows. It will also help to link to something that explains what lies behind the words "something consistent". 4.8. LINEARIZABILITY A concurrent object is a data object shared by concurrent processes. Linearizability is a correctness condition for concurrent objects that exploits the semantics of abstract data types. It permits a high degree of concurrency, yet it permits programmers to specify and reason about concurrent objects using known techniques from the sequential domain. Linearizability provides the illusion that each operation applied by concurrent processes takes effect instantaneously at some point between its invocation and its response, implying that the meaning of a concurrent object‟s operations can be given by pre- and post-conditions. 4.9. JAVA MEMORY MODEL: The Java memory model describes how threads in the Java programming language interact through memory. Together with the description of single-threaded execution of code,
  • 18. the memory model provides the semantics of the Java programming language. The Java programming language and platform provide thread capabilities. Synchronization between threads is notoriously difficult for developers; this difficulty is compounded because Java applications can run on a wide range of processors and operating systems. To be able to draw conclusions about a program's behavior, Java's designers decided they had to clearly define possible behaviors of all Java programs. On modern platforms, code is frequently not executed in the order it was written. It is reordered by the compiler, the processor and the memory subsystem to achieve maximum performance. On multiprocessor architectures, individual processors may have their own local caches that are out of sync with main memory. It is generally undesirable to require threads to remain perfectly in sync with one another because this would be too costly from a performance point of view. This means that at any given time, different threads may see different values for the same shared data. In a single-threaded environment, it is easy to reason about code execution. The typical approach requires the system to implement as-if-serial semantics for individual threads in isolation. When an individual thread executes, it will appear as if all of the actions taken by that thread occur in the order they appear in the program, even if the actions themselves occur out of order. If one thread executes its instructions out of order, then another thread might see the fact that those instructions were executed out of order, even if that did not affect the semantics of the first thread. For example, consider two threads with the following instructions, executing concurrently, where the variables x and y are both initialized to 0: Thread 1 Thread 2 x = 1; int r1 = y; y = 2; int r2 = x; If no reorderings are performed, and the read of y in Thread 2 returns the value 2, then the subsequent read of x should return the value 1, because the write to x was performed before the write to y. However, if the two writes are reordered, then the read of y can return the value 2, and the read of x can return the value 0. The Java Memory Model (JMM) defines the allowable behavior of multithreaded programs, and therefore describes when such reorderings are possible. It places execution-time constraints on the relationship between threads and main memory in order to achieve consistent and reliable Java applications. By doing this, it makes it possible to reason about code execution in a multithreaded environment, even in the face of optimizations performed by the dynamic compiler, the processor(s) and the caches. For execution of a single thread, the rules are simple. The Java Language Specification requires a Java Virtual Machine to observe within-thread as-if-serial semantics. The runtime (which, in this case, usually refers to the dynamic compiler, the processor and the memory subsystem) is free to introduce any useful execution optimizations as long as the result of the thread in isolation is guaranteed to be exactly the same as it would have been had all the
  • 19. statements been executed in the order the statements occurred in the program (also called program order). The major caveat of this is that as-if-serial semantics do not prevent different threads from having different views of the data. The memory model provides clear guidance about what values are allowed to be returned when the data are read. The basic rules imply that individual actions can be reordered, as long as the as-if-serial semantics of the thread are not violated, and actions that imply communication between threads, such as the acquisition or release of a lock, ensure that actions that happen prior to them are seen by other threads that see their effects. For example, everything that happens before the release of a lock will be seen to be ordered before and visible to everything that happens after a subsequent acquisition of that same lock. Mathematically, there is a partial order called the happens-before order over all actions performed by the program. The happens-before order subsumes the program order; if one action occurs before another in the program order, it will occur before the other in the happens-before order. In addition, releases and subsequent acquires of locks form edges in the happens-before graph. A read is allowed to return the value of a write if that write is the last write to that variable before the read along some path in the happens-before order, or if the write is not ordered with respect to that read in the happens-before order. The Java memory model was the first attempt to provide a comprehensive memory model for a popular programming language.