#memory #concurrency #performance #lock-free #atomic


A safe, dependency-less abstraction for typed atomic smart pointers

7 stable releases

1.3.0 Mar 13, 2023
1.2.1 Nov 5, 2022
1.1.0 Dec 12, 2021
1.0.2 Feb 18, 2021
1.0.0 Nov 5, 2020

#141 in Concurrency

Download history 19/week @ 2022-12-06 16/week @ 2022-12-13 15/week @ 2022-12-20 10/week @ 2022-12-27 4/week @ 2023-01-03 15/week @ 2023-01-10 9/week @ 2023-01-17 22/week @ 2023-01-24 21/week @ 2023-01-31 15/week @ 2023-02-07 33/week @ 2023-02-14 12/week @ 2023-02-21 22/week @ 2023-02-28 21/week @ 2023-03-07 16/week @ 2023-03-14 17/week @ 2023-03-21

76 downloads per month
Used in supergit


207 lines


A safe wrapper around atomic pointers to build datastructures and lock-free algorithms on top of. Only uses std as a dependency.

The standard library contains an AtomicPtr type, which by itself is very unergonomic to use, because it deals with raw pointers. It is very easy to misuse this API and get memory leaks or double-free.

This library makes some assumptions about how you would like to use your data. Furthermore, it is dangerous to use raw pointers without some other memory management strategy. This crate uses Arc<T> to handle reference lifetime management. Data in an AtomPtr<T> is also always heap allocated.

Following is a simple example.

use atomptr::AtomPtr;

struct MyData { name: String }
let data = MyData { name: "Kookie".into() };

let a = AtomPtr::new(data);
println!("Name is: {}", a.get_ref().name);

let old_ref = a.swap(MyData { name: "Bob".into() });
println!("Name now is: {}, was {}", a.get_ref().name, old_ref.name);

Because all data is wrapped by Arc<T> internally, returning a reference of the underlying data doesn't mean it is removed from the pointer itself, or de-allocated after Ref<T> goes out of scope. Another thread can of course swap the data contained in the pointer, which is why compare_exchange requires a previously held data reference.


This micro-library is free software, and licensed under the GNU General Public License, version 3.0 or (at your choice), any later version.

Additional Permissions: For Submission to the Apple App Store: Provided that you are otherwise in compliance with the GPLv3 for each covered work you convey (including without limitation making the Corresponding Source available in compliance with Section 6 of the GPLv3), the qaul.net developers also grant you the additional permission to convey through the Apple App Store non-source executable versions of the Program as incorporated into each applicable covered work as Executable Versions only under the Mozilla Public License version 2.0.

A copy of both the GPL-3.0 and MPL-2.0 license texts are included in this repository.

No runtime deps