Joseph-Matteo-Scorsone/Zig-LockFreeQueue
Zig Library that implements a Michael-Scott lock free queue with the use of Atomic types.
A high-performance, lock-free queue implementation in Zig, based on the Michael-Scott algorithm. Designed for concurrent applications requiring efficient, thread-safe FIFO (First-In-First-Out) data structures.
Lock-Free Design: Utilizes atomic operations to enable multiple threads to enqueue and dequeue without traditional locking mechanisms.
Michael-Scott Algorithm: Implements the well-known non-blocking queue algorithm for safe concurrent access.
Generic Implementation: Supports queues of any data type through Zig's generics.
Allocator Agnostic: Compatible with any allocator conforming to Zig's Allocator interface.
Minimal Dependencies: Built solely with Zig's standard library, ensuring ease of integration.
git clone https://github.com/Joseph-Matteo-Scorsone/Zig-LockFreeQueue.git
const lockfree_queue = b.createModule(.{
.root_source_file = b.path("Zig-LockFreeQueue/src/lockFreeQueue.zig"),
});
exe_mod.addImport("lockfree_queue", lockfree_queue);
Here's a basic example of how to use Zig-LockFreeQueue:
const std = @import("std");
const LockFreeQueue = @import("lockfree_queue").LockFreeQueue;
pub fn main() !void {
const allocator = std.heap.page_allocator;
var queue = try LockFreeQueue(i32).init(allocator);
defer queue.deinit();
try queue.enqueue(42);
std.debug.print("Value: {any}\n", .{queue.peek()});
std.debug.print("Dequeued value: {any}\n", .{queue.dequeue()});
std.debug.print("Queue is empty.\n", .{});
}
init(allocator: *Allocator) !LockFreeQueue(T)
Initializes a new lock-free queue for type T using the provided allocator.
deinit(self: *LockFreeQueue(T)) void
Deinitializes the queue, releasing any allocated resources.
enqueue(self: *LockFreeQueue(T), value: T) !void
Adds a value to the end of the queue.
peek(self: *LockFreeQueue(T)) ?void
Returns the value at the front of the queue without removing it, or null if the queue is empty.
dequeue(self: *LockFreeQueue(T)) ?T
Removes and returns a value from the front of the queue. Returns null if the queue is empty.
zig build test
Ensure that all tests pass to verify the correctness of the implementation.
Contributions are welcome! If you have suggestions, bug reports, or enhancements, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.