System & concurrency

thread.h

Mutexes, scoped locks, semaphores, asynchronous calls, and thread groups.

C++23 mc/thread.h
#include <mc/thread.h>

Keep lock objects and captured state alive until all participating threads finish. Timeouts expressed as double are in seconds.

CLock, CReadLock, and CWriteLock hold a lock for their entire scope. The corresponding guards allow release and reacquisition.

Pass CLocked to a guard to adopt an already-held lock. Join a CThreadGroup explicitly before destroying it.

Jump to a declaration · 84

CMutex

class CMutex

Methods

lock

void lock() const;

Acquires exclusive ownership, blocking until available. The mutex is not recursive.

tryLock

bool tryLock() const;

Attempts acquisition without waiting and returns whether it succeeded. Unlock only after a successful acquisition.

unlock

void unlock() const;

Releases one acquisition held by the calling thread. Prefer a scope guard to pair this with locking.

std

std::mutex& std();

Returns the underlying standard mutex by reference for APIs requiring its native C++ type.

CTimedMutex

Methods

lock

void lock() const;
bool lock(double secs) const;
template<class R, class P> bool lock(const std::chrono::duration<R, P>& dt) const;

Acquires exclusive ownership, blocking until available. Timed overloads return true on acquisition and false on timeout; numeric durations are in seconds. The mutex is not recursive.

tryLock

bool tryLock() const;

Attempts acquisition without waiting and returns whether it succeeded. Unlock only after a successful acquisition.

unlock

void unlock() const;

Releases one acquisition held by the calling thread. Prefer a scope guard to pair this with locking.

std

std::timed_mutex& std();

Returns the underlying standard mutex by reference for APIs requiring its native C++ type.

CRecMutex

class CRecMutex

Methods

lock

void lock() const;
bool lock(double secs) const;
template<class R, class P> bool lock(const std::chrono::duration<R, P>& dt) const;

Acquires exclusive ownership, blocking until available. Timed overloads return true on acquisition and false on timeout; numeric durations are in seconds. The owning thread may acquire recursively, but must unlock once per acquisition.

tryLock

bool tryLock() const;

Attempts acquisition without waiting and returns whether it succeeded. Unlock only after a successful acquisition.

unlock

void unlock() const;

Releases one acquisition held by the calling thread. Prefer a scope guard to pair this with locking.

std

std::recursive_timed_mutex& std();

Returns the underlying standard mutex by reference for APIs requiring its native C++ type.

CRWMutex

class CRWMutex

Methods

readLock

void readLock() const;

Acquires shared ownership, allowing other readers while excluding writers.

tryReadLock

bool tryReadLock() const;

Attempts shared acquisition without waiting and returns whether it succeeded.

writeLock

void writeLock() const;

Acquires exclusive ownership, waiting for both readers and writers to release the mutex.

tryWriteLock

bool tryWriteLock() const;

Attempts exclusive acquisition without waiting and returns whether it succeeded.

readUnlock

void readUnlock() const;

Releases shared ownership held by the current thread. It must match a successful read lock.

writeUnlock

void writeUnlock() const;

Releases exclusive ownership held by the current thread. It must match a successful write lock.

CLockedFlag

Free functions & types

Types, constants & data

constexpr CLockedFlag CLocked;

Functions

cAsync

template<class F> inline auto cAsync(F&& f);

Launches a callable on an asynchronous thread and returns its std::future. Use get() to observe its result or exception; destruction of the last future can wait for completion.

CGuard

template<CLockable T> class CGuard

Methods

CGuard

CGuard(const T& t);
CGuard(T& t, CLockedFlag);
CGuard(const CGuard&) = delete;

Acquires an exclusive lock and releases it at scope exit. The CLocked overload adopts a lock already held by the caller instead of acquiring it again.

release

void release();

Unlocks early and disables automatic release until reacquired. Call only while this guard currently owns the lock.

acquire

void acquire();

Reacquires the lock after an earlier release(), restoring automatic release at scope exit.

tryAcquire

bool tryAcquire();

Attempts reacquisition without waiting and returns whether it succeeded. Call only when the guard does not currently own the lock.

CLock

template<CLockable T> class CLock

Methods

CLock

CLock(const T& t);
CLock(const CLock&) = delete;

Acquires an exclusive lock immediately and releases it on destruction. The referenced lockable object must outlive this scope guard.

CReadGuard

template<CReadLockable T> class CReadGuard

Methods

release

void release();

Unlocks early and disables automatic release until reacquired. Call only while this guard currently owns the lock.

acquire

void acquire();

Reacquires the lock after an earlier release(), restoring automatic release at scope exit.

tryAcquire

bool tryAcquire();

Attempts reacquisition without waiting and returns whether it succeeded. Call only when the guard does not currently own the lock.

CReadLock

template<CReadLockable T> class CReadLock

Methods

CReadLock

explicit CReadLock(const T& t);
CReadLock(const CReadLock&) = delete;

Acquires a shared read lock immediately and releases it on destruction. The referenced lockable object must outlive this scope guard.

CWriteGuard

template<CWriteLockable T> class CWriteGuard

Methods

release

void release();

Unlocks early and disables automatic release until reacquired. Call only while this guard currently owns the lock.

acquire

void acquire();

Reacquires the lock after an earlier release(), restoring automatic release at scope exit.

tryAcquire

bool tryAcquire();

Attempts reacquisition without waiting and returns whether it succeeded. Call only when the guard does not currently own the lock.

CWriteLock

template<CWriteLockable T> class CWriteLock

Methods

CWriteLock

explicit CWriteLock(const T& t);
CWriteLock(const CWriteLock&) = delete;

Acquires an exclusive write lock immediately and releases it on destruction. The referenced lockable object must outlive this scope guard.

CSemaphore

Methods

acquire

template<class R, class P> bool acquire(const std::chrono::duration<R, P>& dt);
bool acquire(double dt);
bool acquire();

Waits for and consumes one permit. Returns false when disabled or a timed wait expires; numeric timeouts are seconds. In the finished state it returns true without consuming a permit.

tryAcquire

bool tryAcquire();

Attempts to consume a permit without waiting. Returns false when disabled or no permit is available, and true in the finished state.

release

void release();
void release(size_t n);

Adds one or n permits and wakes waiting acquirers. It does not change an enabled, disabled, or finished state.

enable

void enable();

Returns to normal permit-counting behavior. This setter is unsynchronized; use it only when concurrent operations have stopped.

disable

void disable();

Wakes waiters and makes acquisition fail.

finish

void finish();

Wakes waiters and makes acquisition succeed without consuming permits.

reset

void reset(int count);

Replaces the permit count and re-enables the semaphore. This setter is unsynchronized and does not notify waiters; use it while the semaphore is quiescent.

CThreadGroup

Methods

start

template<class... Args> void start(size_t threadCount, Args&&... args);

Starts the requested number of threads with the supplied callable and arguments and sets the cooperative active flag. Previous threads must already be joined before starting another group.

active

bool active();

Reports the cooperative run flag. Worker loops must check it themselves; clearing it does not interrupt a blocked thread.

stop

void stop();

Clears the cooperative active flag; workers must observe it. Does not join threads.

shutdown

void shutdown();

Clears the active flag and joins all threads.

await

void await();

Joins all threads without changing the active flag.

size

size_t size() const;

Returns the number of thread objects awaiting a join, including threads whose callable has already returned.