Skip to content

Thread

ll/api/thread/ Β· Common

Overview

The Thread module provides thread pool executors, server thread executors, and synchronization utilities for concurrent programming.

Headers

Header Description
ll/api/thread/ThreadPoolExecutor.h Thread pool executor
ll/api/thread/ServerThreadExecutor.h Server main thread executor
ll/api/thread/GlobalThreadPauser.h Pause all game threads
ll/api/thread/ThreadName.h Thread naming utility

Key Classes

ThreadPoolExecutor

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
namespace ll::thread {
class ThreadPoolExecutor : public coro::Executor {
public:
    explicit ThreadPoolExecutor(std::string name, size_t nThreads = 1);

    void resize(size_t nThreads);
    void destroy();

    void execute(std::function<void()>) const override;
    std::shared_ptr<data::CancellableCallback> executeAfter(std::function<void()>, Duration) const override;

    static ThreadPoolExecutor const& getDefault();
};
}

ServerThreadExecutor

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
namespace ll::thread {
class ServerThreadExecutor : public coro::Executor {
public:
    ServerThreadExecutor(std::string name, Duration maxOnceDuration, size_t checkPack);

    void execute(std::function<void()>) const override;
    std::shared_ptr<data::CancellableCallback> executeAfter(std::function<void()>, Duration) const override;

    static ServerThreadExecutor const& getDefault();
};
}

Usage

Thread Pool Execution

C++
1
2
3
4
5
6
7
8
9
#include "ll/api/thread/ThreadPoolExecutor.h"

void runInBackground() {
    auto& executor = ll::thread::ThreadPoolExecutor::getDefault();

    executor.execute([] {
        // This runs in a background thread
    });
}

Server Thread Execution

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include "ll/api/thread/ServerThreadExecutor.h"

void runOnServerThread() {
    auto& executor = ll::thread::ServerThreadExecutor::getDefault();

    executor.execute([] {
        // This runs on the server main thread
        // Safe to access game state here
    });
}

Delayed Execution

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include "ll/api/thread/ThreadPoolExecutor.h"
#include <chrono>

void scheduleTask() {
    auto& executor = ll::thread::ThreadPoolExecutor::getDefault();

    auto callback = executor.executeAfter(
        [] {
            // Runs after 5 seconds
        },
        std::chrono::seconds(5)
    );

    // Can cancel the callback
    // callback->cancel();
}

Custom Thread Pool

C++
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include "ll/api/thread/ThreadPoolExecutor.h"

void createCustomPool() {
    ll::thread::ThreadPoolExecutor myPool("MyPool", 4);  // 4 threads

    myPool.execute([] {
        // Work
    });

    myPool.resize(8);  // Resize to 8 threads
}
  • Coroutine β€” Executors are used by coroutines
  • Chrono β€” Duration types for delayed execution