P45 Imminent
P45 Imminent

Reputation: 8601

A lower level of std::atomic<unsigned int>

I have a struct that consists of plain old data which I share between two dynamic linked libraries (or shared objects). (Compiled with C++11).

One of the data members has to be an atomic type or, to be more precise, I need to be able to apply prefix ++ and -- to it atomically.

I have concerns using std::atomic<unsigned int> for the member since I think that will tie the two libraries to using the same STL implementation.

So I'd rather use std::uint32_t as the member and apply atomic operations to that member within the libraries. Only I can't figure out how to do that, other than using a mutex which would degrade performance to an unacceptable degree.

In summary how can I do something like

std::int32_t foo;
atomic_increment(foo);

using functions available by standard C++11?

Upvotes: 0

Views: 198

Answers (1)

Sebastian Redl
Sebastian Redl

Reputation: 72063

You can't. Standard C++ doesn't respect the idea of different library implementations in different shared libraries (since it doesn't have a concept of the latter) and therefore does not consider your situation to exist. Therefore, there is no facility to support it.

In practice, though, just use std::atomic - any decent compiler/library on a platform that actually supports atomics should leave no trace of it at the machine code.

Upvotes: 1

Related Questions