easy-to-use lock-free atomic integers and booleans for multithreading
Project description
atomicx
atomicx is an easy-to-use atomics library for Python, providing atomic integer and boolean operations. It allows you to perform atomic operations on shared variables, ensuring thread-safety and preventing race conditions in concurrent programming. Everything is entirely lock-free and is backed by Rust's atomic types.
Features
- Atomic integer operations: load, store, add, subtract, swap, compare and exchange, multiply, divide, increment, decrement.
- Atomic boolean operations: load, store, swap, compare and exchange, flip.
Installation
Binary wheels are provided for Python 3.7 and above on Linux, macOS, and Windows:
pip install atomicx
Usage
See the documentation for more information. Here's a quick overview:
Atomic Integer
from atomicx import AtomicInt
# Create an atomic integer with an initial value of 0
atom = AtomicInt()
# Perform atomic operations
atom.store(10)
value = atom.load()
print(f"Value: {value}")
previous_value = atom.swap(20)
print(f"Previous Value: {previous_value}")
atom.add(5)
print(f"Result after addition: {atom}")
# Increment and decrement operations
atom.inc()
atom.dec()
Atomic Boolean
from atomicx import AtomicBool
# Create an atomic boolean with an initial value of False
atom = AtomicBool()
# Perform atomic operations
atom.store(True)
value = atom.load()
print(f"Value: {value}")
previous_value = atom.swap(False)
print(f"Previous Value: {previous_value}")
result = atom.compare_exchange(False, True)
print(f"Swap Result: {result}")
# Flip the value of the atomic boolean
atom.flip()
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
- The
atomicx
library is heavily dependent on and inspired by the Ruststd::sync::atomic
module.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
atomicx-0.0.12.tar.gz
(9.2 kB
view hashes)
Built Distributions
Close
Hashes for atomicx-0.0.12-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f4c350117eb78fcc9901c11015a2e0984b4f7628c8ab714321b4740322637c3 |
|
MD5 | b02c2e96cbced06ffe1b350845738308 |
|
BLAKE2b-256 | 5b4db5557070fd3dd65d257e1dd431e88eab3648655403cfd7f49698a4076b9d |
Close
Hashes for atomicx-0.0.12-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b6c01d28f2c88a48bcf6cd58d37d59d81c14080e93956c40145e30225b234c9 |
|
MD5 | 0b8b93dd95fa4c7031dd9654c0230574 |
|
BLAKE2b-256 | e207170bf6e12c78b7a89a8710aa76a563437ffa21b0a8c3fe1f6ec64d7ecddb |
Close
Hashes for atomicx-0.0.12-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 429ab90d958a71eb63d706b5da9c4b04122a130cb146d1b418e5e2d13d33e83b |
|
MD5 | 8b5f618f3c593e7730acbc553bfce3b1 |
|
BLAKE2b-256 | 5bcdef9e394bf07ae6c1fadca8c3e94b7c09a81115885327fe2ec456e4e629de |