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
Start()— deferred start; call only once. A second call throws.Join()— wait for thread to finishIsAliveManagedThreadId— unique thread IDSleep(ms)— static method
System::Threading::Thread t([]{
// thread body
std::cout << "Running\n";
});
t.Start();
t.Join();
Synchronization
| Class | Header | Status | Description |
|---|---|---|---|
Mutex | Threading/Mutex.hpp | Done | Mutual exclusion lock |
Monitor | Threading/Monitor.hpp | Done | Monitor.Enter / Exit / Wait / Pulse |
Lock | Threading/Lock.hpp | Done | C# 13-style lock object |
SemaphoreSlim | Threading/SemaphoreSlim.hpp | Done | Lightweight semaphore |
Semaphore | Threading/Semaphore.hpp | Done | Named/unnamed semaphore |
ManualResetEvent | Threading/ManualResetEvent.hpp | Done | Wait handle |
AutoResetEvent | Threading/AutoResetEvent.hpp | Done | Auto-resetting wait handle |
ManualResetEventSlim | Threading/ManualResetEventSlim.hpp | Done | Lightweight manual reset event |
SpinLock | Threading/SpinLock.hpp | Done | Spin-wait lock |
SpinWait | Threading/SpinWait.hpp | Done | Spin-wait helper |
ReaderWriterLockSlim | Threading/ReaderWriterLockSlim.hpp | Done | Reader-writer lock |
Interlocked | Threading/Interlocked.hpp | Done | Atomic 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):
| Class | Status | Notes |
|---|---|---|
Task<T> | Partial | Wraps std::future |
ValueTask<T> | Partial | Lightweight task |
TaskCompletionSource<T> | Partial | Manual task completion |
Parallel | Partial | Parallel.For, Parallel.ForEach |
Other Threading Types
| Class | Header | Description |
|---|---|---|
ThreadLocal<T> | Threading/ThreadLocal.hpp | Thread-local storage |
AsyncLocal<T> | Threading/AsyncLocal.hpp | Async-context-local storage |
PeriodicTimer | Threading/PeriodicTimer.hpp | Periodic async timer |
CountdownEvent | Threading/CountdownEvent.hpp | Count-down latch |
Barrier | Threading/Barrier.hpp | Thread barrier |
LazyInitializer | Threading/LazyInitializer.hpp | Lazy 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.