MainContextPusher
Description:
Push this as the new thread-default main context for the current thread, using [method@GLib.
MainContext.push_thread_default], and return a new [alias@GLib.MainContextPusher]. Pop with free. Using [method@GLib.MainContext.pop_thread_default] on this while a [alias@GLib.MainContextPusher] exists for it can lead to undefined behaviour.
Using two [alias@GLib.MainContextPusher]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
}
Parameters:
| this |
a main context to push |