Threading

Thread, synchronization primitives, CancellationToken, Timer, and Tasks.

ℹ Implementation note
Threading primitives are backed by C++ <thread>, <mutex>, <atomic>, and <condition_variable>. No async/await support. Thread safety is not universally guaranteed — check per-class documentation.

System::Threading::Thread

include/System/Threading/Thread.hpp

Status: Done

System::Threading::Thread t([]{
    // thread body
    std::cout << "Running\n";
});
t.Start();
t.Join();

Synchronization

ClassHeaderStatusDescription
MutexThreading/Mutex.hppDoneMutual exclusion lock
MonitorThreading/Monitor.hppDoneMonitor.Enter / Exit / Wait / Pulse
LockThreading/Lock.hppDoneC# 13-style lock object
SemaphoreSlimThreading/SemaphoreSlim.hppDoneLightweight semaphore
SemaphoreThreading/Semaphore.hppDoneNamed/unnamed semaphore
ManualResetEventThreading/ManualResetEvent.hppDoneWait handle
AutoResetEventThreading/AutoResetEvent.hppDoneAuto-resetting wait handle
ManualResetEventSlimThreading/ManualResetEventSlim.hppDoneLightweight manual reset event
SpinLockThreading/SpinLock.hppDoneSpin-wait lock
SpinWaitThreading/SpinWait.hppDoneSpin-wait helper
ReaderWriterLockSlimThreading/ReaderWriterLockSlim.hppDoneReader-writer lock
InterlockedThreading/Interlocked.hppDoneAtomic add/compare-exchange etc.

CancellationToken

include/System/Threading/CancellationToken.hpp | src/System/Threading/CancellationToken.cpp

Status: Done

System::Threading::CancellationTokenSource cts;
auto token = cts.getTokenProperty();

// In a thread:
while (!token.getIsCancellationRequestedProperty()) {
    // do work
}

// Cancel from another thread:
cts.Cancel();

Timer

include/System/Threading/Timer.hpp

Status: Done

Dangling-this UB fixed via shared_ptr<State>.

System::Threading::Timer t([](std::any) {
    std::cout << "tick\n";
}, nullptr,
/*dueTime=*/1000,    // ms before first fire
/*period=*/500);     // ms between fires

ThreadPool

include/System/Threading/ThreadPool.hpp

Queues work items to a thread pool. Status: Partial

Tasks

include/System/Threading/Tasks/

Task-based async patterns (no async/await syntax):

ClassStatusNotes
Task<T>PartialWraps std::future
ValueTask<T>PartialLightweight task
TaskCompletionSource<T>PartialManual task completion
ParallelPartialParallel.For, Parallel.ForEach

Other Threading Types

ClassHeaderDescription
ThreadLocal<T>Threading/ThreadLocal.hppThread-local storage
AsyncLocal<T>Threading/AsyncLocal.hppAsync-context-local storage
PeriodicTimerThreading/PeriodicTimer.hppPeriodic async timer
CountdownEventThreading/CountdownEvent.hppCount-down latch
BarrierThreading/Barrier.hppThread barrier
LazyInitializerThreading/LazyInitializer.hppLazy initialization helpers
⚠ Thread safety not universal
Not all sharp-runtime types are thread-safe. Verify thread safety per-class. Concurrent access to shared std::string, std::vector, or custom types requires external synchronization.