Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

Asserting binary representations for atomic<T>

montagftb
Beginner
319 Views
Hi,

I'm looking to utilize the atomic TBB construct across a library API I am developing. I am aware of the C++ ABI dragons that be there (and can work around them), but I do not know very much about the binary layout of the atomic data structure.

In order to make sure I can use atomic in my interface, I'd like to find out if I can assert a fixed binary representation for atomic where T is a known size (e.g., 16- or 32-bit integral).

Is this achievable? Are there compiler settings I need to be aware of that could throw off this assertion (e.g., alignment and/or padding settings)? I'm using gcc on the Mac and MSVC on Windows. Any help here would be greatly appreciated. Thanks in advance!
0 Kudos
3 Replies
robert-reed
Valued Contributor II
319 Views

A "fixed binary representation" as in "two-complement, little-endian integer?" I'm not sure I get the gist of your question. If you haven't already, you might take a look at the TBBopen source release of include/tbb/atomic.h. You'll find the base type for atomic is atomic_base:

template struct atomic_base { I my_value; };

There are template specializations for various word sizes and for void * and bool, plus a bunch of hair to play nice with various compilers, so atomic covers a variety of word sizes and binary representations. If you're worried about alignment of 16- or 32-bit values, I wouldn't and the only padding issue I can think of would concern over false sharing of adjacent atomics that occupy the same cache line when pulled in from memory.

Is this an answer to your question?

0 Kudos
ARCH_R_Intel
Employee
319 Views

On all current commercially supported platforms, atomic has an alignment andlayout that exactly matches the layout for T.Our regression test test_atomic.cpp checks this property.

There's no guarantee of this property for future platforms, because we are at the mercy of whatever the C++ compiler does, but I would beamazed if a production C++ compiler had a different layout for an atomic than a T.

0 Kudos
montagftb
Beginner
319 Views
Thanks for the answers to my question -- they were both helpful!

Blessings,
Foster
0 Kudos
Reply