Cond

class Cond(pointer: <Error class: unknown class><<Error class: unknown class>>) : Record

The #GCond struct is an opaque data structure that represents a condition. Threads can block on a #GCond if they find a certain condition to be false. If other threads change the state of this condition they signal the #GCond, and that causes the waiting threads to be woken up.

Consider the following example of a shared variable. One or more threads can wait for data to be published to the variable and when another thread publishes the data, it can signal one of the waiting threads to wake up to collect the data.

Here is an example for using GCond to block a thread until a condition is satisfied: |[ gpointer current_data = NULL; GMutex data_mutex; GCond data_cond;

void push_data (gpointer data) { g_mutex_lock (&data_mutex); current_data = data; g_cond_signal (&data_cond); g_mutex_unlock (&data_mutex); }

gpointer pop_data (void) { gpointer data;

g_mutex_lock (&data_mutex);
while (!current_data)
  g_cond_wait (&data_cond, &data_mutex);
data = current_data;
current_data = NULL;
g_mutex_unlock (&data_mutex);

return data;

} ]| Whenever a thread calls pop_data() now, it will wait until current_data is non-null, i.e. until some other thread has called push_data().

The example shows that use of a condition variable must always be paired with a mutex. Without the use of a mutex, there would be a race between the check of @current_data by the while loop in pop_data() and waiting. Specifically, another thread could set

Constructors

Link copied to clipboard
constructor(pointer: <Error class: unknown class><<Error class: unknown class>>)

Types

Link copied to clipboard
object Companion : RecordCompanion<Cond, <Error class: unknown class>>

Properties

Link copied to clipboard
val glibCondPointer: <Error class: unknown class><<Error class: unknown class>>

Functions

Link copied to clipboard
fun broadcast()

If threads are waiting for @cond, all of them are unblocked. If no threads are waiting for @cond, this function has no effect. It is good practice to lock the same mutex as the waiting threads while calling this function, though not required.

Link copied to clipboard
fun clear()

Frees the resources allocated to a #GCond with g_cond_init().

Link copied to clipboard
fun init()

Initialises a #GCond so that it can be used.

Link copied to clipboard
fun signal()

If threads are waiting for @cond, at least one of them is unblocked. If no threads are waiting for @cond, this function has no effect. It is good practice to hold the same lock as the waiting thread while calling this function, though not required.