GLib.MainContext.pusher_new

### Method

# GLib MainContextpusher_new

since: 2.64

#### Declaration [src]

    GMainContextPusher*
    g_main_context_pusher_new (
      GMainContext* main_context
    )

#### Description [src]

Push `main_context` as the new thread-default main context for the current thread, using `g_main_context_push_thread_default()`, and return a new `GMainContextPusher`. Pop with g_main_context_pusher_free(). Using `g_main_context_pop_thread_default()` on `main_context` while a `GMainContextPusher` exists for it can lead to undefined behaviour.

Using two `GMainContextPusher`s in the same scope is not allowed, as it leads to an undefined pop order.

This is intended to be used with g_autoptr(). Note that `g_autoptr()` is only available when using GCC or clang, so the following example will only work with those compilers:

    typedef struct
    {
      ...
      GMainContext *context;
      ...
    } MyObject;

    static void
    my_object_do_stuff (MyObject *self)
    {
      g_autoptr(GMainContextPusher) pusher = g_main_context_pusher_new (self->context);

      // Code with main context as the thread default here

      if (cond)
        // No need to pop
        return;

      // Optionally early pop
      g_clear_pointer (&pusher, g_main_context_pusher_free);

      // Code with main context no longer the thread default here
    }

Available since: 2.64

#### Return value

Type: `GMainContextPusher`

A `GMainContextPusher`.

The caller of the method takes ownership of the returned data, and is responsible for freeing it.