Skip to main content

easy-to-use lock-free atomic integers and booleans for multithreading

Project description

atomicx

PyPI version

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 Rust std::sync::atomic module.

Project details


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)

Uploaded Source

Built Distributions

atomicx-0.0.12-cp37-abi3-win_amd64.whl (137.6 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

atomicx-0.0.12-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

atomicx-0.0.12-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (512.4 kB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page