# Glib::MainContext Class Reference
[The Main Event Loop]

Main context. More...

`#include <glibmm/main.h>`

List of all members.

## Public Types

typedef Glib::MainContext | CppObjectType
typedef GMainContext | BaseObjectType

## Public Member Functions

bool | iteration (bool may_block)
| Runs a single iteration for the given main loop.
bool | pending ()
| Checks if any sources have pending events for the given context.
void | wakeup ()
| If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.
bool | acquire ()
| Tries to become the owner of the specified context.
bool | wait (Glib::Cond& cond, Glib::Mutex& mutex)
| Tries to become the owner of the specified context, as with acquire().
void | release ()
| Releases ownership of a context previously acquired by this thread with acquire().
bool | prepare (int& priority)
| Prepares to poll sources within a main loop.
bool | prepare ()
| Prepares to poll sources within a main loop.
void | query (int max_priority, int& timeout, std::vector< PollFD >& fds)
| Determines information necessary to poll this main loop.
bool | check (int max_priority, std::vector< PollFD >& fds)
| Passes the results of polling back to the main loop.
void | dispatch ()
| Dispatches all pending sources.
void | set_poll_func (GPollFunc poll_func)
| Sets the function to use to handle polling of file descriptors.
GPollFunc | get_poll_func ()
| Gets the poll function set by g_main_context_set_poll_func().
void | add_poll (PollFD& fd, int priority)
| Adds a file descriptor to the set of file descriptors polled for this context.
void | remove_poll (PollFD& fd)
| Removes file descriptor from the set of file descriptors to be polled for a particular context.
SignalTimeout | signal_timeout ()
| Timeout signal, attached to this MainContext.
SignalIdle | signal_idle ()
| Idle signal, attached to this MainContext.
SignalIO | signal_io ()
| I/O signal, attached to this MainContext.
SignalChildWatch | signal_child_watch ()
| child watch signal, attached to this MainContext.
void | reference () const
void | unreference () const
GMainContext* | gobj ()
const GMainContext* | gobj () const
GMainContext* | gobj_copy () const

## Static Public Member Functions

static Glib::RefPtr< MainContext > | create ()
| Creates a new MainContext.
static Glib::RefPtr< MainContext > | get_default ()
| Returns the default main context.

## Related Functions

(Note that these are not member functions.)
Glib::RefPtr< MainContext > | wrap (GMainContext* gobject, bool take_copy=false)

## Detailed Description

Main context.

## Member Typedef Documentation

typedef GMainContext Glib::MainContext::BaseObjectType

typedef Glib::MainContext Glib::MainContext::CppObjectType

## Member Function Documentation

bool Glib::MainContext::acquire | ( |  | ) |

Tries to become the owner of the specified context.

If some other context is the owner of the context, returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when release() is called as many times as acquire(). You must be the owner of a context before you can call prepare(), query(), check(), dispatch().

Returns:
  true if the operation succeeded, and this thread is now the owner of context.

void Glib::MainContext::add_poll | ( | PollFD& | fd,
|  | int | priority |
| ) |  |  |

Adds a file descriptor to the set of file descriptors polled for this context.

This will very seldomly be used directly. Instead a typical event source will use Glib::Source::add_poll() instead.

Parameters:

  | fd | A PollFD structure holding information about a file descriptor to watch.
  | priority | The priority for this file descriptor which should be the same as the priority used for Glib::Source::attach() to ensure that the file descriptor is polled whenever the results may be needed.

bool Glib::MainContext::check | ( | int | max_priority,
|  | std::vector< PollFD >& | fds |
| ) |  |  |

Passes the results of polling back to the main loop.

Parameters:

  | max_priority | Maximum numerical priority of sources to check.
  | fds | Vector of Glib::PollFD's that was passed to the last call to query()

Returns:
  true if some sources are ready to be dispatched.

static Glib::RefPtr<MainContext> Glib::MainContext::create | ( |  | ) | `[static]`

Creates a new MainContext.

Returns:
  The new MainContext.

void Glib::MainContext::dispatch | ( |  | ) |

Dispatches all pending sources.

static Glib::RefPtr<MainContext> Glib::MainContext::get_default | ( |  | ) | `[static]`

Returns the default main context.

This is the main context used for main loop functions when a main loop is not explicitly specified.

Returns:
  The new MainContext.

GPollFunc Glib::MainContext::get_poll_func | ( |  | ) |

Gets the poll function set by g_main_context_set_poll_func().

Returns:
  The poll function

GMainContext* Glib::MainContext::gobj | ( |  | ) |

const GMainContext* Glib::MainContext::gobj | ( |  | ) | const

GMainContext* Glib::MainContext::gobj_copy | ( |  | ) | const

bool Glib::MainContext::iteration | ( | bool | may_block | ) |

Runs a single iteration for the given main loop.

This involves checking to see if any event sources are ready to be processed, then if no events sources are ready and may_block is true, waiting for a source to become ready, then dispatching the highest priority events sources that are ready. Note that even when may_block is true, it is still possible for iteration() to return false, since the the wait may be interrupted for other reasons than an event source becoming ready.

Parameters:

  | may_block | Whether the call may block.

Returns:
  true if events were dispatched.

bool Glib::MainContext::pending | ( |  | ) |

Checks if any sources have pending events for the given context.

Returns:
  true if events are pending.

bool Glib::MainContext::prepare | ( | int & | priority | ) |

Prepares to poll sources within a main loop.

The resulting information for polling is determined by calling query().

Parameters:

  | priority | Location to store priority of highest priority source already ready.

Returns:
  true if some source is ready to be dispatched prior to polling.

bool Glib::MainContext::prepare | ( |  | ) |

Prepares to poll sources within a main loop.

The resulting information for polling is determined by calling query().

Returns:
  true if some source is ready to be dispatched prior to polling.

void Glib::MainContext::query | ( | int | max_priority,
|  | int & | timeout,
|  | std::vector< PollFD >& | fds |
| ) |  |  |

Determines information necessary to poll this main loop.

Parameters:

  | max_priority | Maximum priority source to check.
  | timeout | Location to store timeout to be used in polling.
  | fds | Location to store Glib::PollFD records that need to be polled.

Returns:
  the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored.

void Glib::MainContext::reference | ( |  | ) | const

void Glib::MainContext::release | ( |  | ) |

Releases ownership of a context previously acquired by this thread with acquire().

If the context was acquired multiple times, the only release ownership when release() is called as many times as it was acquired.

void Glib::MainContext::remove_poll | ( | PollFD& | fd | ) |

Removes file descriptor from the set of file descriptors to be polled for a particular context.

Parameters:

  | fd | A PollFD structure holding information about a file descriptor.

void Glib::MainContext::set_poll_func | ( | GPollFunc | poll_func | ) |

Sets the function to use to handle polling of file descriptors.

It will be used instead of the poll() system call (or GLib's replacement function, which is used where poll() isn't available). This function could possibly be used to integrate the GLib event loop with an external event loop.

Parameters:

  | poll_func | The function to call to poll all file descriptors.

SignalChildWatch Glib::MainContext::signal_child_watch | ( |  | ) |

child watch signal, attached to this MainContext.

Returns:
  A signal proxy; you want to use SignalChildWatch::connect().

SignalIdle Glib::MainContext::signal_idle | ( |  | ) |

Idle signal, attached to this MainContext.

Returns:
  A signal proxy; you want to use SignalIdle::connect().

SignalIO Glib::MainContext::signal_io | ( |  | ) |

I/O signal, attached to this MainContext.

Returns:
  A signal proxy; you want to use SignalIO::connect().

SignalTimeout Glib::MainContext::signal_timeout | ( |  | ) |

Timeout signal, attached to this MainContext.

Returns:
  A signal proxy; you want to use SignalTimeout::connect().

void Glib::MainContext::unreference | ( |  | ) | const

bool Glib::MainContext::wait | ( | Glib::Cond& | cond,
|  | Glib::Mutex& | mutex |
| ) |  |  |

Tries to become the owner of the specified context, as with acquire().

But if another thread is the owner, atomically drop mutex and wait on cond until that owner releases ownership or until cond is signaled, then try again (once) to become the owner.

Parameters:

  | cond | A condition variable.
  | mutex | A mutex, currently held.

Returns:
  true if the operation succeeded, and this thread is now the owner of context.

void Glib::MainContext::wakeup | ( |  | ) |

If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.