Introducing AtomicIntegerMBS: Thread-Safe Integer Operations in Xojo

With the release of MBS Xojo Plugins version 25.2, developers now have access to a powerful new class for multithreaded programming: AtomicIntegerMBS. This class brings low-level atomic operations into your Xojo toolkit, enabling thread-safe manipulation of integer values across all major platforms (macOS, Windows, Linux, and iOS).

Whether you’re managing counters, flags, or other shared numeric resources between threads, AtomicIntegerMBS ensures consistent and safe updates without requiring traditional locking mechanisms like Mutex.

What Is AtomicIntegerMBS?

AtomicIntegerMBS is a utility class that wraps an Int64 value with atomic operations. This means changes to the integer are guaranteed to happen without interruption, even when accessed from multiple threads. It’s ideal for building efficient, low-overhead concurrent systems.

Core capabilities include:

  • Atomic Reads/Writes via the Value property
  • Arithmetic operations: Add, Subtract, Increment, Decrement
  • Bitwise logic: BitwiseAnd, BitwiseOr, BitwiseXOr
  • Atomic exchange: Exchange
  • Compare-and-swap with CompareExchange

A Real-World Example: Generating Unique IDs

A common use case is assigning unique IDs in a multi-threaded environment. Here’s how AtomicIntegerMBS makes this safe and simple:

Var a As New AtomicIntegerMBS

Do
  Var expected As Int64 = a
  Var newCount As Int64 = expected + 1
  Var success As Boolean = a.CompareExchange(expected, newCount)

  If success Then
    // We successfully reserved our unique ID
    Exit
  End If
Loop

This code safely increments a shared counter, even if multiple threads are executing it simultaneously.

Key Methods and Features

CompareExchange(expected, desired) As Boolean

Atomically sets the value to desired only if the current value equals expected. This is the cornerstone of many lock-free algorithms.

Increment() / Decrement()

Quickly and safely increment or decrement the value. Returns the new value after the operation.

Add(value As Int64) / Subtract(value As Int64)

Atomically add or subtract from the current value. Returns the previous value before the operation.

Exchange(value As Int64) As Int64

Replaces the current value with a new one and returns the previous value. Useful for atomic swapping.

Bitwise Operations

Thread-safe bitwise operations:

  • BitwiseAnd(value)
  • BitwiseOr(value)
  • BitwiseXOr(value)

Operator Overloads

To make working with AtomicIntegerMBS feel more natural, the class includes operator overloads for:

  • Arithmetic: +, -
  • Comparison: <, >, =
  • Conversion to/from Int64

Example:

Var a As AtomicIntegerMBS = 5
Var b As Int64 = a + 3
If a < 10 Then
  MessageBox("Still under limit.")
End If

Thread Safety Without Locks

What makes AtomicIntegerMBS especially attractive is its ability to avoid traditional locking mechanisms. Mutexes are useful but come with performance overhead and potential deadlocks. Atomic operations, by contrast, allow for lock-free programming patterns that are fast and scalable.

This is particularly valuable in real-time applications, background processing, or when building thread-safe counters, toggles, or other shared numeric state.

Conclusion

The addition of AtomicIntegerMBS in MBS Xojo Plugin 25.2 is a big win for developers building concurrent applications. It brings safe, efficient atomic operations to your fingertips, supporting all Xojo targets.

If you’re working on any multi-threaded logic, you owe it to yourself to explore this class—it may be the key to simplifying your thread management code and improving performance.

See also: AtomicFlagMBS for simple atomic boolean flags.

3 Likes