Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the C++ 11 standard guarantees that std::atomic<> is implemented as a lock-free operation?

Tags:

I'm at a junction, I'm trying to pick one between mutex-lock-based data structure and lock-free ( and possibly wait-free ) data structure.

While digging a little deeper I found no word about whether the C++11 standard supports lock-free operations for atomic types, not even for width-based integrals like atomic_uint32_t . In other words it's not just the std::atomic<> interface that is not granted to be lock-free; the only thing that looks like it's granted to be lock-free in the whole standard library is std::atomic_flag .

Is this true or I'm missing something ? What is the reason for that ? I mean the standard calls "atomic" something that is clearly not lock-free at all and it's something that is even allowed to use mutexes or blocking calls under the hood.

like image 711
user2485710 Avatar asked Nov 11 '13 06:11

user2485710


People also ask

Is STD atomic lock-free?

std::atomic<T>::is_lock_freeChecks whether the atomic operations on all objects of this type are lock-free.

What is atomic lock C++?

A spin lock is just a single atomic integer (or boolean) with two values: LOCKED and UNLOCKED. The locking function does an atomic compare and swap in a while loop, until it successfully modifies the value from UNLOCKED to LOCKED, at which point this thread owns the lock.

Is std :: atomic thread safe?

Yes, it would be threadsafe. Assuming of course there are no bugs in the std::atomic implementation - but it's not usually hard to get right. This is exactly what std::atomic is meant to do.

Do lock-free algorithms use atomic instructions?

The key in lock-free programming is to use hardware-intrinsic atomic operations. As a matter of fact, even locks themselves must use those atomic operations!


2 Answers

The C++ standard makes no guarantee that std::atomic<T> operations are lock-free. However, you can use std::atomic<T>::is_lock_free() to find out if the operation of std::atomic<T> is lock free 29.6.5 [atomics.types.operations.req] paragraph 7:

Returns: True if the object’s operations are lock-free, false otherwise.

If it is not lock-free it will still do the required synchronization but it uses some lock to do so.

like image 73
Dietmar Kühl Avatar answered Oct 06 '22 23:10

Dietmar Kühl


If by atomic you mean, using hardware support without locks, then yes, the standard doesn't give you a guarantee for that. Why? Well, because different architectures support different kind of hardware atomicity. std::atomic<> has the handy is_lock_free() method which can be used to check wether the given object is actually lock free, or uses a lock internally to guarantee atomic operations. You can use that and check on your target hardware wether it would be lock free or not, and then decide what data structure to go for.

However, that being said, if the target architecture has hardware support for atomic operations for the fixed width integrals you are interested in, and you didn't obtain your copy of the standard library from the the shady software shop in the ghetto, it's probably going to use the hardware instead of a full blown lock.

like image 34
JustSid Avatar answered Oct 06 '22 22:10

JustSid