LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-19-2010, 04:10 AM   #1
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Rep: Reputation: 20
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

Last edited by sinu_nayak2001; 05-19-2010 at 04:11 AM.
 
Old 05-19-2010, 06:56 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by sinu_nayak2001 View Post
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.
 
Old 05-19-2010, 07:46 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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.
 
  


Reply

Tags
programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows API atomic operation compatibility in Linux lennyk Programming 1 03-01-2009 08:17 PM
pipes - reading buffer less than PIPE_BUF is atomic operation? gal_netanel Programming 2 12-18-2007 12:28 AM
Question on asm/atomic.h Lothar Schwab Programming 0 08-15-2007 07:13 PM
Atomic operation question PatrickNew Programming 4 05-12-2007 12:11 AM
bad: scheduling while atomic! ??? okeyla Linux - Newbie 1 09-26-2005 02:41 PM


All times are GMT -5. The time now is 07:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration