Atomically increment/decrement a variable in threaded application

  • Follow


Hi,

In a threaded application is there a way to increment/decrement a
variable in an atomic way without using synchronization mechanisms
like mutexes.

Thanks and Regards,
M Shetty
0
Reply mshetty 12/24/2003 1:53:22 PM

In article <bfbb8fd4.0312240553.3324330b@posting.google.com>, mshetty <mshetty@mail.com> wrote:
>
> In a threaded application is there a way to increment/decrement a
> variable in an atomic way without using synchronization mechanisms
> like mutexes.

Not without dropping to assembly code, _if_ the CPU in question has an
atomic increment or decrement instruction... and even then, I wouldn't say
it would necessarily always work reliably on SMP platforms because *some*
SMP CPUs has a lazy memory update type of scheme for greater performance.

A recent one I read about had that issue... POWER4? Point being, don't make
explicit assumptions even if CPU synchronization primitives are available.

IIRC, SPARC v9 doesn't have an atomic increment or decrement instruction.
Nearest would be the v9-specific 'CAS' (compare and swap) instruction but
the exact approach would depend on exactly what version of the SPARC
architecture (v7, v8, v9) your code has to run on.

In the long run, it will likely be far more maintainable for your code if
you stick with library-based synchronization routines unless you have some
really extraordinary needs in which you must hand-code a lot of special
cases, anyway.

-Dan
0
Reply Dan 1/21/2004 7:45:42 AM


1 Replies
586 Views

(page loaded in 0.024 seconds)

Similiar Articles:





7/21/2012 6:08:12 AM


Reply: