LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Atomic variable Vs. Atomic operation (https://www.linuxquestions.org/questions/programming-9/atomic-variable-vs-atomic-operation-808785/)

sinu_nayak2001 05-19-2010 04:10 AM

Atomic variable Vs. Atomic operation
 
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.

Code:

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:

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

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

Sincerely,
Srinivas Nayak

Sergei Steshenko 05-19-2010 06:56 AM

Quote:

Originally Posted by sinu_nayak2001 (Post 3973801)
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.

Code:

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:

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

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

Sincerely,
Srinivas Nayak

Well, if atomic variable is a variable which guarantees atomic read-modify-write operation, then it's a mean to implement semaphores/mutexes.

bigearsbilly 05-19-2010 07:46 AM

well, if such a thing as atomic exists the two pieces of example code are still not equivalent
as you are doing two atomic operations not one.
so if a and b are dependent as implied, then you still need a mutex.

that's what I reckon anyway.


All times are GMT -5. The time now is 10:37 PM.