From: Srinu on
Hi,

Lets say I have two shared variables a,b which are related to each
other. When multiple applications share these shared variables, access
to them needs to be an atomic operation, otherwise the relation may
break. So to ensure mutual exclusion, I'll put their modification
under a critical section protected by lock.

critical_code
{
P(mutex)
a := something
b := something
V(mutex)
}

Lets say my hardware/OS/compiler supports atomic variables. Then I
modified my above code as follows.

code
{
atomic a := something
atomic b := something
}

Can this code ensure mutual exclusion, when accessed by multiple
applications?

Sincerely,
Srinivas Nayak
From: Tim Little on
On 2010-05-19, Srinu <sinu.nayak2001(a)gmail.com> wrote:
> Lets say my hardware/OS/compiler supports atomic variables. Then I
> modified my above code as follows.
>
> code
> {
> atomic a := something
> atomic b := something
> }
>
> Can this code ensure mutual exclusion, when accessed by multiple
> applications?

In a sense yes, but at a much more fine-grained scale than you want.
It ensures that neither "a" nor "b" will be partially updated(*) as
viewed from another context of execution, but does nothing to ensure
that they are consistent with each other. It does not even
necessarily ensure that "b" is only seen to be updated after "a",
although some specific implementations of atomic variables may
do that as well.

(*) By partially updated, consider an implementation that writes to a
32-bit integer by writing the bottom 16 bits into memory and then the
top 16 bits, with the possibility of a context switch or another CPU
accessing the variable in between.


- Tim