Synchronization » CnxCondvar module

CnxCondvar provides a higher-level condition variable type, directly comparable to C++'s std::condition_variable, that helps to make writing efficient multi-threaded code simpler and easier

Example:

#include <Cnx/sync/Mutex.h>
#include <Cnx/sync/UniqueLock.h>
#include <Cnx/sync/Condvar.h>
bool flag;
CnxCondvar condvar;
CnxMutex mutex;

// thread1:
UniqueLock lock = cnx_unique_lock(mutex);
while(!flag) {
    cnx_condvar_wait(&condvar, &lock);
}
// do something now that we've received the flag

// thread2:
// do some stuff
{
    UniqueLock lock = cnx_unique_lock(muted);
    flag = true;
    // signal to thread1 that we're done and it can do its work now
    cnx_condvar_notify_one(&condvar);
}

Classes

struct CnxCondvar
CnxCondvar provides a higher-level condition variable type, directly comparable to C++'s std::condition_variable, that helps to make writing efficient multi-threaded code simpler and easier.

Enums

enum CnxCondvarStatus { CnxCondvarNoTimeout = 0, CnxCondvarTimeout }
Used to communicate the timeout status for cnx_condvar_wait_for and cnx_condvar_wait_until. If a timeout occurs during either of these functions, then CnxCondvarTimeout will be returned. Otherwise CnxCondvarTimeout will be returned.

Functions

CnxCondvar cnx_condvar_new(void)
Creates a new CnxCondvar
void cnx_condvar_wait(CnxCondvar*restrict condvar, struct CnxUniqueLock*restrict lock)
Immediately unlocks lock and waits on the given CnxCondvar, condvar until it has been notified by another thread. lock will be automatically relocked before this function returns.
CnxCondvarStatus cnx_condvar_wait_for(CnxCondvar*restrict condvar, struct CnxUniqueLock*restrict lock, CnxDuration duration)
Immediately unlocks lock and waits on the given CnxCondvar, condvar until it has been notified by another thread, or the amount of time specified by duration has passed. lock will be automatically relocked before this function returns.
CnxCondvarStatus cnx_condvar_wait_until(CnxCondvar*restrict condvar, struct CnxUniqueLock*restrict lock, CnxTimePoint stop_point)
Immediately unlocks lock and waits on the given CnxCondvar, condvar until it has been notified by another thread, or point in time specified by stop_point has occurred. lock will be automatically relocked before this function returns.
void cnx_condvar_notify_one(CnxCondvar*restrict condvar)
Notifies exactly one thread waiting on the given CnxCondvar to stop waiting and continue execution.
void cnx_condvar_notify_all(CnxCondvar*restrict condvar)
Notifies all threads waiting on the given CnxCondvar to stop waiting and continue execution.
void cnx_condvar_free(CnxCondvar*restrict condvar)
Destroys the given CnxCondvar, freeing any associated resources.

Enum documentation

enum CnxCondvarStatus

Used to communicate the timeout status for cnx_condvar_wait_for and cnx_condvar_wait_until. If a timeout occurs during either of these functions, then CnxCondvarTimeout will be returned. Otherwise CnxCondvarTimeout will be returned.

Function documentation

CnxCondvar cnx_condvar_new(void)

Creates a new CnxCondvar

Returns a new CnxCondvar

void cnx_condvar_wait(CnxCondvar*restrict condvar, struct CnxUniqueLock*restrict lock)

Immediately unlocks lock and waits on the given CnxCondvar, condvar until it has been notified by another thread. lock will be automatically relocked before this function returns.

Parameters
condvar - The condvar to wait on
lock - The exclusive lock associated with the condition

CnxCondvarStatus cnx_condvar_wait_for(CnxCondvar*restrict condvar, struct CnxUniqueLock*restrict lock, CnxDuration duration)

Immediately unlocks lock and waits on the given CnxCondvar, condvar until it has been notified by another thread, or the amount of time specified by duration has passed. lock will be automatically relocked before this function returns.

Parameters
condvar - The condvar to wait on
lock - The exclusive lock associated with the condition
duration - The maximum amount of time to wait on the condition
Returns The CnxCondvarStatus indicating whether the thread was notified (CnxCondvarNoTimeout), or, a timeout occurred and duration passed before a notification was received (CnxCondvarTimeout).

CnxCondvarStatus cnx_condvar_wait_until(CnxCondvar*restrict condvar, struct CnxUniqueLock*restrict lock, CnxTimePoint stop_point)

Immediately unlocks lock and waits on the given CnxCondvar, condvar until it has been notified by another thread, or point in time specified by stop_point has occurred. lock will be automatically relocked before this function returns.

Parameters
condvar - The condvar to wait on
lock - The exclusive lock associated with the condition
stop_point - The point in time at which to stop waiting on the condition
Returns The CnxCondvarStatus indicating whether the thread was notified (CnxCondvarNoTimeout), or, a timeout occurred and stop_point occurred before a notification was received (CnxCondvarTimeout).

void cnx_condvar_notify_one(CnxCondvar*restrict condvar)

Notifies exactly one thread waiting on the given CnxCondvar to stop waiting and continue execution.

Parameters
condvar - The condvar to notify a waiting thread to continue

void cnx_condvar_notify_all(CnxCondvar*restrict condvar)

Notifies all threads waiting on the given CnxCondvar to stop waiting and continue execution.

Parameters
condvar - The condvar to notify waiting threads to continue

void cnx_condvar_free(CnxCondvar*restrict condvar)

Destroys the given CnxCondvar, freeing any associated resources.

Parameters
condvar - The condvar to destroy