Concurrency

Threads, mutexes, condition variables, queues and processes. More...

Classes

class  Condition
 Signal and wait synchronisation primitive. More...
class  Mutex
 Mutual exclusion device. More...
class  MutexLock
 Scoped lock for a Mutex. More...
class  RecursiveMutex
 Recursive mutual exclusion device. More...
class  RecursiveLock
 Scoped lock for a RecursiveMutex. More...
class  ReadWriteMutex
 Mutex for concurrent readers or one writer. More...
class  ReadLock
 Scoped read lock for a ReadWriteMutex. More...
class  WriteLock
 Scoped write lock for a ReadWriteMutex. More...
class  SpinMutex
 Spin mutex. More...
class  SpinLock
 Scoped lock for a SpinMutex. More...
class  ProcessFailed
 Thrown when a process does not terminate normally. More...
class  ProcessInfo
 Startup parameters for a Process. More...
class  Process
 Starts another program as a child process. More...
class  Queue< T >
 Thread-safe FIFO queue. More...
class  Semaphore
 Counting semaphore. More...
class  Thread
 Portable thread of control. More...
class  AttachedThread
 Thread that joins in its destructor. More...
class  DetachedThread
 Thread that runs without a waiter. More...
class  atomic_t
 Atomic integers to be used with atomicity functions. More...

Functions

int atomicGet (volatile atomic_t &val)
 Atomically get a value.
void atomicSet (volatile atomic_t &val, int n)
 Atomically set a value.
int atomicIncrement (volatile atomic_t &val)
 Increases a value by one as an atomic operation.
int atomicDecrement (volatile atomic_t &val)
 Decreases a value by one as an atomic operation.
int atomicExchange (volatile atomic_t &val, int exch)
 Performs an atomic exchange operation.
int atomicCompareExchange (volatile atomic_t &val, int exch, int comp)
 Performs an atomic compare-and-exchange operation.
int atomicExchangeAdd (volatile atomic_t &val, int add)
 Performs atomic addition of two values.
void * atomicExchange (void *volatile &val, void *exch)
 Performs an atomic exchange operation.
void * atomicCompareExchange (void *volatile &val, void *exch, void *comp)
 Performs an atomic compare-and-exchange operation.

Detailed Description

A process can run more than one thread of control. Thread is the portable thread. Construction does not start it. start() runs a Callable or an EventLoop. The thread must be joined or detached before it is destroyed. AttachedThread joins in its destructor. DetachedThread runs without a waiter and destroys itself when the entry returns.

Mutex serializes access to shared data. It is not recursive: the same thread must not lock it again. MutexLock locks in the constructor and unlocks in the destructor, including during stack unwinding. RecursiveMutex allows the owning thread to lock again. ReadWriteMutex allows concurrent readers or one writer. SpinMutex is for short critical sections. Atomic integers are documented with the core module.

Condition waits while a Mutex or MutexLock is held. wait() unlocks, suspends the caller, and relocks when the wait ends. signal() wakes one waiter. broadcast() wakes all. Semaphore counts. wait() decrements when the count is positive. post() increments.

Queue is a thread-safe FIFO. get() blocks while the queue is empty. put() blocks when a maximum size is set and the queue is full. A maximum of zero means no limit.

Process starts another program from ProcessInfo. start() runs it. wait() joins it. Redirected stdin, stdout, and stderr are IODevice endpoints.

Function Documentation

◆ atomicGet()

int atomicGet ( volatile atomic_t & val)
related

Returns the value and employs a memory fence after the get. Acquire semantics prevent memory reordering with any read or write operation which follows it in program order.

◆ atomicSet()

void atomicSet ( volatile atomic_t & val,
int n )
related

Sets the value and employs a memory fence before the set. Release semantics prevent memory reordering with any read or write operation which precedes it in program order.

◆ atomicIncrement()

int atomicIncrement ( volatile atomic_t & val)
related

Returns the resulting incremented value.

◆ atomicDecrement()

int atomicDecrement ( volatile atomic_t & val)
related

Returns the resulting decremented value.

◆ atomicExchange() [1/2]

int atomicExchange ( volatile atomic_t & val,
int exch )
related

Sets val to exch and returns the initial value of val.

◆ atomicCompareExchange() [1/2]

int atomicCompareExchange ( volatile atomic_t & val,
int exch,
int comp )
related

If val is equal to comp, val is replaced by exch. The initial value of of val is returned.

◆ atomicExchangeAdd()

int atomicExchangeAdd ( volatile atomic_t & val,
int add )
related

Returns the initial value of the addend.

◆ atomicExchange() [2/2]

void * atomicExchange ( void *volatile & val,
void * exch )
related

Sets val to exch and returns the initial value of val.

◆ atomicCompareExchange() [2/2]

void * atomicCompareExchange ( void *volatile & val,
void * exch,
void * comp )
related

If val is equal to comp, val is replaced by exch. The initial value of ptr is returned.