---
url: /logly.zig/examples/thread-pool.md
description: >-
  Example of parallel log processing with Logly.zig thread pool. Configure
  worker threads, work stealing, batch submission, and priority-based task
  scheduling.
---

# Thread Pool Example

This example demonstrates parallel log processing using Logly's thread pool.

## Centralized Configuration

```zig
const logly = @import("logly");

var config = logly.Config.default();
config.threadPool = .{
    .enabled = true,
    .threadCount = 8,
    .queueSize = 2048,
    .stackSize = 1024 * 1024,
};

// Or use helper method
var config2 = logly.Config.default().withThreadPool(.{
    .threadCount = 4,
    .queueSize = 1024,
});
```

## Source Code

```zig
//! Thread Pool Example
//!
//! Demonstrates parallel log processing with configurable thread pools.

const std = @import("std");
const logly = @import("logly");

pub fn main() !void {
    var gpa = std.heap.DebugAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // Create thread pool with 4 workers using explicit config
    const pool = try logly.ThreadPool.initWithConfig(allocator, .{
        .threadCount = 4,
        .queueSize = 1024,
        .workStealing = true,
    });
    defer pool.deinit();

    // Start workers
    try pool.start();

    // Submit tasks
    var counter: std.atomic.Value(u32) = std.atomic.Value(u32).init(0);
    const numTasks: u32 = 100;

    const TestTask = struct {
        fn increment(ctx: *anyopaque, maybeAllocator: ?std.mem.Allocator) void {
            _ = maybeAllocator;
            const c: *std.atomic.Value(u32) = @ptrCast(@alignCast(ctx));
            _ = c.fetchAdd(1, .monotonic);
        }
    };

    for (0..numTasks) |_| {
        _ = pool.submitCallback(TestTask.increment, @ptrCast(&counter));
    }

    // Wait for completion
    pool.waitAll();

    // Check results
    std.debug.print("Counter value: {d}\n", .{counter.load(.monotonic)});

    const stats = pool.getStats();
    std.debug.print("Tasks completed: {d}\n", .{
        stats.getCompleted(),
    });
}
```

## Running the Example

```bash
zig build run-thread-pool-demo
```

## Expected Output

```
Counter value: 101
Tasks completed: 101
```

## Key Concepts

### Thread Pool Configuration

```zig
.threadCount = 4,      // Number of worker threads
.queueSize = 1024,     // Queue size per thread
.workStealing = true,  // Enable work stealing
.enable_priorities = true, // Priority queues
```

### Task Priorities

```zig
.priority = .low,      // Background tasks
.priority = .normal,   // Regular tasks
.priority = .high,     // Important tasks
.priority = .critical, // Must process first
```

### Using Presets

```zig
// Single thread (for testing)
const single = logly.ThreadPoolPresets.singleThread();

// CPU-bound workloads
const cpu = logly.ThreadPoolPresets.cpuBound();

// I/O-bound workloads
const io = logly.ThreadPoolPresets.ioBound();

// Maximum throughput
const high = logly.ThreadPoolPresets.highThroughput();
```

### Parallel Sink Writing

```zig
var writer = try logly.ParallelSinkWriter.init(allocator, .{
    .maxConcurrent = 4,
});

try writer.addSink(&fileSink);
try writer.addSink(&console_sink);

// Write to all sinks in parallel
try writer.write(&record);
```

## Statistics

```zig
const stats = pool.getStats();
std.debug.print("Completed: {d}\n", .{stats.getCompleted()});
std.debug.print("Stolen: {d}\n", .{stats.getStolen()});
std.debug.print("Throughput: {d:.2}/sec\n", .{stats.throughput()});
```

## See Also

* [Thread Pool API](../api/thread-pool.md)
* [Thread Pool Guide](../guide/thread-pool.md)
* [Async Logging Example](async-logging.md)

## New Methods (v0.0.9)

```zig
var pool = try logly.ThreadPool.init(allocator, config);
defer pool.deinit();

// State methods
const empty = pool.isEmpty();
const full = pool.isFull();

// Performance metrics
const util = pool.utilization();  // 0.0 - 1.0

// Reset statistics
pool.resetStats();
```

## Aliases

| Alias | Method |
|-------|--------|
| `flush` | `clear` |
| `statistics` | `getStats` |
| `stop` | `shutdown` |
| `halt` | `shutdown` |
| `begin` | `start` |
| `add` | `submit` |
