[Gllug] Programming: Atomic Increment/Decrement
Jonathan Dye
jonathan.dye at automationpartnership.com
Wed Aug 7 13:27:22 UTC 2002
> -----Original Message-----
> From: Vincent AE Scott [mailto:gllug at codex.net]
> Sent: 07 August 2002 14:12
> To: gllug at linux.co.uk
> Subject: Re: [Gllug] Programming: Atomic Increment/Decrement
>
>
> Jonathan Dye(jonathan.dye at automationpartnership.com)@Wed, Aug
> 07, 2002 at 01:58:10PM +0100:
> > Does anyone know if there are atomic increment/decrement
> functions under
> > linux (or POSIX)?
>
> isnt that what you use a mutex for?
>
> pthread_mutex_init(3)
You _can_ use a mutex to protect the increment/decrement but it is usually
quite heavyweight. A mutex is good if you are doing more than a simple
increment or decrement.
atomic increments/decrement (and for that matter mulitplications, devisions,
assignments, swaps etc) allow you to do things like reference counting in a
multi-threaded environment without the overhead of a mutex. They increment
(or decrement) the counter and return the value (before or after depending
on impl) as a single uninterruptable operation.
If there is no suitable function then I will probably end up writing my own
using mutexes (or the most lightweigh synchronisation method that is
suitable).
JD
_____________________________________________________________________
This message has been checked for all known viruses by the
MessageLabs Virus Scanning Service
--
Gllug mailing list - Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug
More information about the GLLUG
mailing list