System & concurrency
thread.h
Mutexes, scoped locks, semaphores, asynchronous calls, and thread groups.
#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 CMutexMethods
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.
operator=
CTimedMutex
class CTimedMutexMethods
CTimedMutex
CTimedMutex();
CTimedMutex(const CTimedMutex&) = delete;
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.
operator=
CTimedMutex& operator=(const CTimedMutex&) = delete;
CRecMutex
class CRecMutexMethods
CRecMutex
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.
operator=
CRWMutex
class CRWMutexMethods
CRWMutex
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.
operator=
CLockedFlag
struct CLockedFlagFree 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
Methods
CGuard
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.
~CGuard
~CGuard();
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.
operator=
CLock
Methods
CLock
Acquires an exclusive lock immediately and releases it on destruction. The referenced lockable object must outlive this scope guard.
~CLock
~CLock();
operator=
CReadGuard
template<CReadLockable T> class CReadGuardMethods
CReadGuard
explicit CReadGuard(const T& t);
CReadGuard(T& t, CLockedFlag);
CReadGuard(const CReadGuard&) = delete;
Acquires a shared read lock and releases it at scope exit. The CLocked overload adopts a lock already held by the caller instead of acquiring it again.
~CReadGuard
~CReadGuard();
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.
operator=
CReadGuard& operator=(const CReadGuard&) = delete;
CReadLock
template<CReadLockable T> class CReadLockMethods
CReadLock
Acquires a shared read lock immediately and releases it on destruction. The referenced lockable object must outlive this scope guard.
~CReadLock
~CReadLock();
operator=
CWriteGuard
template<CWriteLockable T> class CWriteGuardMethods
CWriteGuard
explicit CWriteGuard(const T& t);
CWriteGuard(const T& t, CLockedFlag);
CWriteGuard(const CWriteGuard&) = delete;
Acquires an exclusive write lock and releases it at scope exit. The CLocked overload adopts a lock already held by the caller instead of acquiring it again.
~CWriteGuard
~CWriteGuard();
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.
operator=
CWriteGuard& operator=(const CWriteGuard&) = delete;
CWriteLock
template<CWriteLockable T> class CWriteLockMethods
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.
~CWriteLock
~CWriteLock();
operator=
CWriteLock& operator=(const CWriteLock&) = delete;
CSemaphore
class CSemaphoreMethods
CSemaphore
explicit CSemaphore(int count = 0);
CSemaphore(const CSemaphore&) = delete;
Creates an enabled semaphore with the supplied permit count, initially zero by default.
~CSemaphore
~CSemaphore();
operator=
CSemaphore& operator=(const CSemaphore&) = delete;
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
class CThreadGroupMethods
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.