Cond


Object Hierarchy:

Object hierarchy for Cond

Description:

[ Version ( since = "2.32" ) ]
[ CCode ( destroy_function = "g_cond_clear" , lvalue_access = false ) ]
public struct Cond

The Cond struct is an opaque data structure that represents a condition.

Threads can block on a Cond if they find a certain condition to be false. If other threads change the state of this condition they signal the Cond, 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 current_data after the check, and signal the cond (with nobody waiting on it) before the first thread goes to sleep. Cond is specifically useful for its ability to release the mutex and go to sleep atomically.

It is also important to use the wait and wait_until functions only inside a loop which checks for the condition to be true. See wait for an explanation of why the condition may not be true even after it returns.

If a Cond is allocated in static storage then it can be used without initialisation. Otherwise, you should call Cond on it and g_cond_clear when done.

A Cond should only be accessed via the g_cond_ functions.

Example: Conditions:

public class Context : Object {
private Cond data_cond = Cond ();
private Mutex data_mutex = Mutex ();
private string current_data = null;

private void push_data (string data) {
print ("=> push_data: lock\n");
data_mutex.lock ();
print ("=> push_data: set data\n");
current_data = data;
data_cond.signal ();
print ("=> push_data: unlock\n");
data_mutex.unlock ();
}

private string pop_data () {
print ("<= pop_data: lock\n");
data_mutex.lock ();
while (current_data == null) {
data_cond.wait (data_mutex);
}
print ("<= pop_data: get data\n");
string data = (owned) current_data;
print ("<= pop_data: unlock\n");
data_mutex.unlock ();
return data;
}

public void run () {
Thread<bool> thread2 = new Thread<bool> ("pop-data", () => {
print ("Thread start:\n");
this.pop_data ();
return true;
});

Thread.usleep (1000000);
this.push_data ("data");
thread2.join ();

// Possible output:
// ``<= pop_data: lock``
// ``=> push_data: lock``
// ``=> push_data: set data``
// ``=> push_data: unlock``
// ``<= pop_data: get data``
// ``<= pop_data: unlock``
}

public static int main (string[] args) {
Context context = new Context ();
context.run ();
return 0;
}
}

valac --target-glib 2.32 --pkg glib-2.0 GLib.Cond.vala


Namespace: GLib
Package: glib-2.0

Content:

Creation methods:

Methods: