LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 01-13-2010, 09:32 AM   #1
silentray
LQ Newbie
 
Registered: Oct 2006
Location: London
Distribution: Gentoo 2008.0
Posts: 23

Rep: Reputation: 15
How to achieve atomic increment/decrement in Linux


Hi,

I am new to Linux programming.

Is there anyway in Linux that can achieve atomic increment/decrement for an integer variable without being interrupted? It means that the thread should not give chance for other thread to run until the increment/decrement is completed.

Please help. Many thanks.

Regards.
 
Old 01-13-2010, 09:36 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Try

Linux semaphore thread

in your favorite WEB search engine.
 
1 members found this post helpful.
Old 01-13-2010, 11:17 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
Try

Linux semaphore thread

in your favorite WEB search engine.
That's not atomic; the locking and unlocking are, but a number of things can reveal an intermediate state.
Kevin Barry
 
Old 01-13-2010, 11:24 AM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
Quote:
semaphore thread
I think semaphore is an oversized solution to protect an increment / decrement operation only. If you're using gcc, take a look here.
 
Old 01-13-2010, 12:59 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
if you are threading you should be using mutexes.
and conditions etc.
 
Old 01-13-2010, 02:15 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ta0kira View Post
That's not atomic; the locking and unlocking are, but a number of things can reveal an intermediate state.
Kevin Barry
Intermediate state of what ?
 
Old 01-13-2010, 05:47 PM   #7
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
If you must have atomic operations rather than using mutexes (Knuth says that "premature optimization is the root of all evil"!), then have a look at how the kernel atomic operations are implemented in C. They are of course processor dependent, not portable.

As far as I know, the Posix API provides no way of doing atomic operations (apart from the normal semaphores etc).

Last edited by neonsignal; 01-13-2010 at 05:51 PM.
 
Old 01-13-2010, 06:34 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
Intermediate state of what ?
  1. lock
  2. increment (??? you might not know what happens here)
  3. unlock
That's at least 3 operations, and mutexes are optional. If by chance incrementing takes more than 1 instruction a signal handler could see it partway done, or another thread could. If, of course, the incrementation is already atomic then there's no problem in the first place, but a semaphore makes it no more atomic.
Kevin Barry
 
Old 01-13-2010, 08:03 PM   #9
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
The question is badly formulated (or it is a bad question).

Code:
i--;
What difference does it make whether or not the sequence before or after the i-- is interrupted? The decrement will most likely be atomic, something like
Code:
decr [bp-16]
What is non-atomic about it?

An atomic operation usually consists of more than one operations which might not be interrupted. Like:
Code:
if (i > 0){
j++;
}
THAT is something which requires special measures if you don't want to have i changed once you made a decision. Therefor the 68000 had instructions like Test And Set, which was atomic by design.

In the example I gave above the more than one instructions, if the must be atomic, must be protected in some way. But from what?

So before any answer can given about how to achieve atomicness (sp?) we would like to know which instructions should be prevented against what.

jlinkels
 
Old 01-13-2010, 08:18 PM   #10
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
The decrement will most likely be atomic
A single processor instruction is atomic in the sense that it cannot be interrupted. However, now that multi-processor/multi-core systems are common (and complex memory caching), you have to be careful about whether the atomicity extends to the memory access. That is why the lock prefix was added to the x86 instruction set.

It is also poor practice to rely on the compiler to produce the same code in every context. There are ways of mitigating this (such as making use of 'volatile'), but it is best to keep such sensitive code inside a library of thread-aware functions, rather than sprinkled through user code.

Last edited by neonsignal; 01-13-2010 at 08:24 PM.
 
3 members found this post helpful.
Old 01-14-2010, 06:33 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ta0kira View Post
  1. lock
  2. increment (??? you might not know what happens here)
  3. unlock
That's at least 3 operations, and mutexes are optional. If by chance incrementing takes more than 1 instruction a signal handler could see it partway done, or another thread could. If, of course, the incrementation is already atomic then there's no problem in the first place, but a semaphore makes it no more atomic.
Kevin Barry
I meant that the same semaphore to be used for both incrementing and reading the value. In such a case if the incrementing thread has grabbed the semaphore, the thread reading the the value being incremented will be able to read it only after the increment is complete and the semaphore is released.

I.e. I was talking about classical critical section protected by semaphore.

...

By the way, the first match in 'Yahoo' gives a link to a Qt function which implements safe increment - and that was the main point of my post.
 
Old 01-14-2010, 06:33 AM   #12
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I think the question is a bit ambiguous, the OP ask for an atomic increment/decrement while most likely he wants to protect this operation from interruption.

Mutexes or semaphores are the way to go. IIRC these functions are part of the threads library in C.

It should not be forgotten that the other thread should use the same mutexes to protect the same variable in the other thread.

Thinking about how a semaphore is decremented in a thread safe-way is a challenge in its own.

jlinkels
 
  


Reply



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
Best way to achieve mailstore redundancy in Linux linux_linux Linux - Newbie 2 03-15-2008 08:53 PM
pam_mount login count doesn't decrement beaucoup Linux - Security 0 08-04-2005 11:08 PM
how to use fork/pipe to decrement a global varialbe Y and exit when Y is 0 keiwu Programming 1 02-19-2005 11:33 PM
Help This Newb Achieve his Linux Dream! TeknoPhreak Linux - Newbie 9 03-21-2003 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration