Skip to content

Commit 5d1c23f

Browse files
author
wintel2014
committed
cppreference
All atomic types except for std::atomic_flag may be implemented using mutexes or other locking operations, rather than using the lock-free atomic CPU instructions. Atomic types are also allowed to be sometimes lock-free, e.g. if only aligned memory accesses are naturally atomic on a given architecture, misaligned objects of the same type have to use locks.
1 parent 95a969b commit 5d1c23f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

C++/C++11/LockFree/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BIN = lockfree
2+
3+
$(BIN): $(BIN).cpp
4+
g++ $^ -o $@ -latomic -std=c++11

C++/C++11/LockFree/lockfree.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <utility>
3+
#include <atomic>
4+
5+
struct A { int a[100]; };
6+
struct B { int x, y; };
7+
struct C { int x, y, z; };
8+
int main()
9+
{
10+
std::cout << std::boolalpha
11+
<< "std::atomic<A> is lock free? "
12+
<< std::atomic<A>{}.is_lock_free() << '\n'
13+
<< "std::atomic<B> is lock free? "
14+
<< std::atomic<B>{}.is_lock_free() << '\n'
15+
<< "std::atomic<C> is lock free? "
16+
<< std::atomic<C>{}.is_lock_free() << '\n';
17+
18+
std::cout << "std::atomic<bool> is lock free? "
19+
<< std::atomic<bool>{}.is_lock_free() << '\n';
20+
}

0 commit comments

Comments
 (0)