1

What are the advantages and disadvantages of using Interlocked winapi functions instead of any library provides atomic operations on Win32 platform?

Portability is not an issue.

ali_bahoo
  • 4,732
  • 6
  • 41
  • 63

3 Answers3

4

If portability is not a concern then you're basically down to deciding whom you trust more to get this right. A library is generally designed to provide portability. It otherwise has a tough time competing with an OS provided implementation that's been battle-hardened for over 15 years.

Check this thread to see an example of how the obvious implementation is not in fact the best.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

The Interlocked winapi functions work on old processors even when there is no CPU support for locked operations. 386 and maybe 486, not really a issue today unless you still support Win9x and older NT.

Anders
  • 97,548
  • 12
  • 110
  • 164
  • They work *differently* on those CPUs though. (they're only atomic with respect to other interlocked operations) – jalf Nov 26 '10 at 15:47
0

It would likely depend up on the specific atomic library in question.

A good library with a specific back-end would likely end up with the same implementation of a couple of ASM instructions to issue an x86 lock instruction and do their work. And assuming the library itself is portable, subsequently make your code portable.

A naive atomic implementation might do something heavier like use a mutex to protect a normal variable. I don't know of any that do - just making the point for argument.

As such, given your stated non-portability requirements, using the Win32 functions should be fine. Alternately, go ahead with an Atomic version, but perhaps look at the actual implementation.

sdg
  • 4,645
  • 3
  • 32
  • 26