MetallicPriest
MetallicPriest

Reputation: 30765

atomic<int> for older c++ compilers

I am using atomic<int> in my code, but the machine in which now I'm compiling has an older g++ version which doesn't support C++11. Is there any equivalent class available on the net, so that I can use it in my code, or if not, where I can find the C++11 implementation of atomic<int> so I can copy it from there. Can this be easily done?

Upvotes: 3

Views: 1195

Answers (3)

ildjarn
ildjarn

Reputation: 62975

The proposed (i.e. unofficial) Boost.Atomic library aims to do exactly this. I don't know what state it's in currently, but it's used in the implementation of the recently (officially) accepted Boost.Lockfree library, so presumably it's usable.


EDIT — updated links, now that both Atomic and Lockfree have officially been in Boost for some time:
Boost.Atomic
Boost.Lockfree

Upvotes: 5

Max Lybbert
Max Lybbert

Reputation: 20039

Hans Boehm's atomic ops library is good, although it's hard to determine what's available on various platforms.

If you're OK with the LGPL, Intel TBB has what you're looking for as well (plus a lot of other stuff).

If you're only looking at GCC, then you may be able to get away with just using GCC's intrinsics (I'm not sure which version of GCC those showed up in, but they've been around for a while).

Upvotes: 2

Paul Rubel
Paul Rubel

Reputation: 27222

sig_atomic_t

This is an integral type of an object that can be accessed as an atomic entity, even in the presence of asynchronous signals.

in gcc is atomic

To avoid uncertainty about interrupting access to a variable, you can use a particular data type for which access is always atomic: sig_atomic_t.

Upvotes: 2

Related Questions