Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

mbitsnbites/atomic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚠️ This repository has moved to: https://gitlab.com/mbitsnbites/atomic

Atomic operations for C++

This is a very lightweight library that implements thread safe, atomic operations for C++. It is portable and easy to use.

CI status

OS master branch
Linux & macOS (GCC, Clang) Build status
Windows (MSVC) Build status

Why avoid std::atomic?

Because <atomic> drags in loads of code in the pre-processing step (over 1MB of code for MSVC 2015). This library, which is based on compiler intrinsics (and a fallback to std::atomic), is much lighter.

Another reason may be that you can not (for whatever reason) use C++11 in your project, but still need portable atomic operations.

Example code

Spinlock with an automatic lock guard

#include "atomic/spinlock.h"

static atomic::spinlock lock;

void foo() {
  atomic::lock_guard guard(lock);

  // Stuff that is synchronized by the lock...
}

This is the generated machine code (clang 3.8, x86_64):

foo:
    movl            $1, %ecx
.spin:
    xorl            %eax, %eax
    lock cmpxchgl   %ecx, lock(%rip)
    jne             .spin

    // Stuff that is synchronized by the lock...

    xorl            %eax, %eax
    xchgl           %eax, lock(%rip)
    retq

License

This is free and unencumbered software released into the public domain.

For more info, see LICENSE.

About

Tiny portable C++ library for atomic operations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published