glibmm 2.40.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Examples

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Glib::Threads::Cond Class Reference
Threads

An opaque data structure to represent a condition. More...

`#include <glibmm/threads.h>`

## Public Member Functions
| Cond ()
| ~Cond ()
void | signal ()
| If threads are waiting for this Cond, exactly one of them is woken up. More...
void | broadcast ()
| If threads are waiting for this Cond, all of them are woken up. More...
void | wait (Mutex& mutex)
| Waits until this thread is woken up on this Cond. More...
bool | wait_until (Mutex& mutex, gint64 end_time)
| Waits until this thread is woken up on this Cond, but not longer than until the time, that is specified by end_time. More...
GCond* | gobj ()

## Detailed Description

An opaque data structure to represent a condition.

A Cond is an object that threads can block on, if they find a certain condition to be false. If other threads change the state of this condition they can signal the Cond, such that the waiting thread is woken up.

Usage example:
  Glib::Cond data_cond;
  Glib::Mutex data_mutex;
  void* current_data = 0;
  void push_data(void* data)
  {
  Glib::Mutex::Lock lock (data_mutex);
  current_data = data;
  data_cond.signal();
  }
  void* pop_data()
  {
  Glib::Mutex::Lock lock (data_mutex);
  while (!current_data)
  data_cond.wait(data_mutex);
  void *const data = current_data;
  current_data = 0;
  return data;
  }

Examples:
  thread/thread.cc.

## Constructor & Destructor Documentation

Glib::Threads::Cond::Cond | ( | ) |  |

Glib::Threads::Cond::~Cond | ( | ) |  |

## Member Function Documentation

void Glib::Threads::Cond::broadcast | ( | ) |  |

If threads are waiting for this Cond, all of them are woken up.

It is good practice to hold the same lock as the waiting thread, while calling this method, though not required.

GCond* Glib::Threads::Cond::gobj | ( | ) |  |
| inline

void Glib::Threads::Cond::signal | ( | ) |  |

If threads are waiting for this Cond, exactly one of them is woken up.

It is good practice to hold the same lock as the waiting thread, while calling this method, though not required.

void Glib::Threads::Cond::wait | ( | Mutex& | mutex) |  |

Waits until this thread is woken up on this Cond.

The mutex is unlocked before falling asleep and locked again before resuming.

Parameters

  mutex | a Mutex that is currently locked.

Note
  It is important to use the wait() and wait_until() methods only inside a loop, which checks for the condition to be true as it is not guaranteed that the waiting thread will find it fulfilled, even if the signaling thread left the condition in that state. This is because another thread can have altered the condition, before the waiting thread got the chance to be woken up, even if the condition itself is protected by a Mutex.

bool Glib::Threads::Cond::wait_until | ( | Mutex& | mutex,
|  | gint64 | end_time
| ) |  |

Waits until this thread is woken up on this Cond, but not longer than until the time, that is specified by end_time.

The mutex is unlocked before falling asleep and locked again before resuming.

Parameters

  mutex | a Mutex that is currently locked.
  end_time | a max time to wait.

Note
  It is important to use the wait() and wait_until() methods only inside a loop, which checks for the condition to be true as it is not guaranteed that the waiting thread will find it fulfilled, even if the signaling thread left the condition in that state. This is because another thread can have altered the condition, before the waiting thread got the chance to be woken up, even if the condition itself is protected by a Mutex.