关于 Vala GLib.Idle

About GLib.Idle on Vala

Valadoc 在某些部分没有很好地记录,GLib 中的命名空间 Idle 没有描述它们是做什么的,只有几个函数来定义空闲事件的优先级别!

有人知道这是做什么的吗?

Functions:

public uint add (owned SourceFunc function, int priority = DEFAULT_IDLE)
public uint add_full (int priority, owned SourceFunc function)
public bool remove_by_data (void* data)

如有疑问,请参阅 C 文档:

https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-idle-add

Adds a function to be called whenever there are no higher priority events pending to the default main loop. The function is given the default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function returns FALSE it is automatically removed from the list of event sources and will not be called again.

See memory management of sources for details on how to handle the return value and memory management of data .

This internally creates a main loop source using g_idle_source_new() and attaches it to the global GMainContext using g_source_attach(), so the callback will be invoked in whichever thread is running that main context. You can do these steps manually if you need greater control or to use a custom main context.

一般来说,您可能想阅读有关主循环的内容:

https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#glib-The-Main-Event-Loop.description

The main event loop manages all the available sources of events for GLib and GTK+ applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets) and timeouts. New types of event sources can also be added using g_source_attach().

To allow multiple independent sets of sources to be handled in different threads, each source is associated with a GMainContext. A GMainContext can only be running in a single thread, but sources can be added to it and removed from it from other threads.

Each event source is assigned a priority. The default priority, G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high priority sources are always processed before events from lower priority sources.

Idle functions can also be added, and assigned a priority. These will be run whenever no events with a higher priority are ready to be processed.

[...]