Introducing the New AtomicFlagMBS Class in MBS Xojo Plugins 25.2
With the release of MBS Xojo Plugins version 25.2, developers gain access to a powerful new class designed to help manage concurrency in a thread-safe, lock-free manner: the AtomicFlagMBS
class. This lightweight atomic boolean type can be the foundation for building your own synchronization primitives like semaphores or critical sections — without relying on heavyweight system locks.
What is AtomicFlagMBS?
AtomicFlagMBS
is a simple yet powerful tool for low-level concurrency control. It wraps a single atomic boolean value and provides atomic operations to read, set, and clear it. Because all operations are performed atomically, this class is safe to use across threads without the risk of data races.
Key Features:
- Lock-free: No system-level locks or mutexes involved.
- Cross-platform: Works on macOS, Windows, Linux, and iOS.
- Lightweight: Ideal for high-performance, low-overhead synchronization.
Typical Use Case: Simple Spin Lock
One of the most common uses of an atomic flag is implementing a spin lock or lightweight lock mechanism:
Var f As New AtomicFlagMBS
// Try to acquire the lock
Var b As Boolean = f.TestAndSet
If Not b Then
// Successfully acquired the lock
Break
// Perform your critical section work here
// Release the lock
f.Clear
Else
// Lock already held
Break
End If
The TestAndSet
method atomically checks the current value of the flag and sets it to True
. It returns the value that was previously held, allowing you to determine if you successfully acquired the lock.
Releasing the Lock
To release the lock, call the Clear
method:
f.Clear
This sets the flag back to False
, making it available again.
When Should You Use AtomicFlagMBS
?
Use this class when:
- You need lightweight, low-latency synchronization.
- You’re implementing custom concurrency tools like semaphores or critical regions.
- You want fine control over multi-threaded access without the cost of full-blown locking.
Related Classes
If you need atomic operations on integers, check out the related AtomicIntegerMBS
class — also included in the MBS Util Plugin.
Available Now in MBS Xojo Plugins 25.2
The AtomicFlagMBS
class is available in MBS Xojo Util Plugin, version 25.2 and supports all major platforms including macOS, Windows, Linux, and iOS.
For more details, examples, and documentation, visit the official MBS Plugin site.
See also Introducing AtomicInteger: Thread-Safe Integer Operations in Xojo and Introducing AtomicQueueMBS – A Thread-Safe Queue for Xojo.