这是用户在 2024-10-29 24:38 为 https://gnome.pages.gitlab.gnome.org/libsoup/glib/glib-The-Main-Event-Loop.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

The Main Event Loop 主事件循环

The Main Event Loop — manages all available sources of events
主事件循环——管理所有可用的事件源

Functions 功能

GMainLoop *
G主循环*
g_main_loop_new ()
g_main_loop_new ()
GMainLoop *
G主循环*
g_main_loop_ref ()
g_main_loop_ref ()
void 空白 g_main_loop_unref ()
g_main_loop_unref ()
void 空白 g_main_loop_run ()
g_main_loop_run ()
void 空白 g_main_loop_quit ()
g_main_loop_quit ()
gboolean 布尔值 g_main_loop_is_running ()
g_main_loop_is_running ()
GMainContext * g_main_loop_get_context ()
g_main_loop_get_context ()
#define g_main_new()
g_main_new ()
#define g_main_destroy()
g_main_destroy ()
#define g_main_run()
g_main_run ()
#define g_main_quit()
g_main_quit ()
#define g_main_is_running()
g_main_is_running ()
GMainContext * g_main_context_new ()
g_main_context_new ()
GMainContext * g_main_context_new_with_flags ()
g_main_context_new_with_flags ()
GMainContext * g_main_context_ref ()
g_main_context_ref ()
void 空白 g_main_context_unref ()
g_main_context_unref ()
GMainContext * g_main_context_default ()
g_main_context_default ()
gboolean 布尔值 g_main_context_iteration ()
g_main_context_iteration ()
#define g_main_iteration()
g_main_iteration ()
gboolean 布尔值 g_main_context_pending ()
g_main_context_pending ()
#define g_main_pending
GSource *
G来源*
g_main_context_find_source_by_id ()
g_main_context_find_source_by_id ()
GSource *
G来源*
g_main_context_find_source_by_user_data ()
g_main_context_find_source_by_user_data ()
GSource *
G来源*
g_main_context_find_source_by_funcs_user_data ()
g_main_context_find_source_by_funcs_user_data ()
void 空白 g_main_context_wakeup ()
g_main_context_wakeup ()
gboolean 布尔值 g_main_context_acquire ()
g_main_context_acquire ()
void 空白 g_main_context_release ()
g_main_context_release ()
gboolean 布尔值 g_main_context_is_owner ()
g_main_context_is_owner ()
gboolean 布尔值 g_main_context_wait ()
g_main_context_wait ()
gboolean 布尔值 g_main_context_prepare ()
g_main_context_prepare ()
gint 金特 g_main_context_query ()
g_main_context_query ()
gboolean 布尔值 g_main_context_check ()
g_main_context_check ()
void 空白 g_main_context_dispatch ()
g_main_context_dispatch ()
void 空白 g_main_context_set_poll_func ()
g_main_context_set_poll_func ()
GPollFunc 轮询函数 g_main_context_get_poll_func ()
g_main_context_get_poll_func ()
gint 金特 (*GPollFunc) ()
( *GPollFunc ) ()
void 空白 g_main_context_add_poll ()
g_main_context_add_poll ()
void 空白 g_main_context_remove_poll ()
g_main_context_remove_poll ()
gint 金特 g_main_depth ()
g_main_深度()
GSource *
G来源*
g_main_current_source ()
g_main_current_source ()
#define g_main_set_poll_func()
g_main_set_poll_func ()
void 空白 g_main_context_invoke ()
g_main_context_invoke ()
void 空白 g_main_context_invoke_full ()
g_main_context_invoke_full ()
GMainContextPusher * g_main_context_pusher_new ()
g_main_context_pusher_new ()
void 空白 g_main_context_pusher_free ()
g_main_context_pusher_free ()
GMainContext * g_main_context_get_thread_default ()
g_main_context_get_thread_default ()
GMainContext * g_main_context_ref_thread_default ()
g_main_context_ref_thread_default ()
void 空白 g_main_context_push_thread_default ()
g_main_context_push_thread_default ()
void 空白 g_main_context_pop_thread_default ()
g_main_context_pop_thread_default ()
GSource *
G来源*
g_timeout_source_new ()
g_timeout_source_new ()
GSource *
G来源*
g_timeout_source_new_seconds ()
g_timeout_source_new_seconds ()
guint 古恩特 g_timeout_add ()
g_timeout_add ()
guint 古恩特 g_timeout_add_full ()
g_timeout_add_full ()
guint 古恩特 g_timeout_add_seconds ()
g_timeout_add_seconds ()
guint 古恩特 g_timeout_add_seconds_full ()
g_timeout_add_seconds_full ()
GSource *
G来源*
g_idle_source_new ()
g_idle_source_new ()
guint 古恩特 g_idle_add ()
g_idle_add ()
guint 古恩特 g_idle_add_full ()
g_idle_add_full ()
gboolean 布尔值 g_idle_remove_by_data ()
g_idle_remove_by_data ()
void 空白 (*GChildWatchFunc) ()
( *GChildWatchFunc ) ()
GSource *
G来源*
g_child_watch_source_new ()
g_child_watch_source_new ()
guint 古恩特 g_child_watch_add ()
g_child_watch_add ()
guint 古恩特 g_child_watch_add_full ()
g_child_watch_add_full ()
gint 金特 g_poll ()
g_poll ()
void 空白 (*GSourceDummyMarshal) ()
( *GSourceDummyMarshal ) ()
void 空白 (*GSourceDisposeFunc) ()
( *GSourceDisposeFunc ) ()
GSource *
G来源*
g_source_new ()
g_source_new ()
GSource *
G来源*
g_source_ref ()
g_source_ref ()
void g_source_unref ()
void g_source_set_funcs ()
void g_source_set_dispose_function ()
guint g_source_attach ()
void g_source_destroy ()
gboolean g_source_is_destroyed ()
void g_source_set_priority ()
gint g_source_get_priority ()
void g_source_set_can_recurse ()
gboolean g_source_get_can_recurse ()
guint g_source_get_id ()
const char * g_source_get_name ()
void g_source_set_name ()
void g_source_set_static_name ()
void g_source_set_name_by_id ()
GMainContext * g_source_get_context ()
void g_source_set_callback ()
gboolean (*GSourceFunc) ()
#define G_SOURCE_FUNC()
void g_source_set_callback_indirect ()
void g_source_set_ready_time ()
gint64 g_source_get_ready_time ()
gpointer g_source_add_unix_fd ()
void g_source_remove_unix_fd ()
void g_source_modify_unix_fd ()
GIOCondition g_source_query_unix_fd ()
void g_source_add_poll ()
void g_source_remove_poll ()
void g_source_add_child_source ()
void g_source_remove_child_source ()
gint64 g_source_get_time ()
void g_source_get_current_time ()
gboolean g_source_remove ()
gboolean g_source_remove_by_funcs_user_data ()
gboolean g_source_remove_by_user_data ()
void (*GClearHandleFunc) ()
void g_clear_handle_id ()
int g_steal_fd ()

Types and Values

Includes 包括

#include <glib.h>

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().
主事件循环管理 GLib 和 GTK+ 应用程序的所有可用事件源。这些事件可以来自任意数量的不同类型的源,例如文件描述符(普通文件、管道或套接字)和超时。还可以使用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. All functions which operate on a GMainContext or a built-in GSource are thread-safe.
为了允许在不同线程中处理多个独立的源集,每个源都与一个GMainContext关联。 GMainContext只能在单个线程中运行,但可以在其他线程中向其中添加源或从中删除源。所有在GMainContext或内置GSource上操作的函数都是线程安全的。

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: if several sources are ready to dispatch, the ones with equal-highest priority will be dispatched on the current GMainContext iteration, and the rest wait until a subsequent GMainContext iteration when they have the highest priority of the sources which are ready for dispatch.
每个事件源都被分配一个优先级。默认优先级G_PRIORITY_DEFAULT为 0。小于 0 的值表示更高的优先级。大于 0 的值表示优先级较低。来自高优先级源的事件始终在来自低优先级源的事件之前处理:如果有多个源准备好分派,则具有相同最高优先级的源将在当前GMainContext迭代中分派,其余的则等到后续GMainContext迭代时再进行处理。具有最高优先级的已准备好调度的源。

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 dispatched.
还可以添加空闲功能,并分配优先级。只要没有更高优先级的事件可供调度,这些事件就会运行。

The GMainLoop data type represents a main event loop. A GMainLoop is created with g_main_loop_new(). After adding the initial event sources, g_main_loop_run() is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns.
GMainLoop数据类型表示主事件循环。 GMainLoop 是使用g_main_loop_new()创建的。添加初始事件源后,调用g_main_loop_run() 。这会持续检查来自每个事件源的新事件并分派它们。最后,对来自其中一个源的事件的处理导致调用g_main_loop_quit()以退出主循环,并且g_main_loop_run()返回。

It is possible to create new instances of GMainLoop recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular GMainContext, and will be checked and dispatched for all main loops associated with that GMainContext.
可以递归地创建GMainLoop的新实例。这通常在 GTK+ 应用程序中显示模式对话框时使用。请注意,事件源与特定的GMainContext关联,并且将为与该 GMainContext 关联的所有主循环进行检查和分派。

GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending().
GTK+ 包含其中一些函数的包装器,例如gtk_main()gtk_main_quit()gtk_events_pending()

Creating new source types
创建新的源类型

One of the unusual features of the GMainLoop functionality is that new types of event source can be created and used in addition to the builtin type of event source. A new event source type is used for handling GDK events. A new source type is created by "deriving" from the GSource structure. The derived type of source is represented by a structure that has the GSource structure as a first element, and other elements specific to the new source type. To create an instance of the new source type, call g_source_new() passing in the size of the derived structure and a table of functions. These GSourceFuncs determine the behavior of the new source type.
GMainLoop功能的不寻常功能之一是,除了内置类型的事件源之外,还可以创建和使用新类型的事件源。新的事件源类型用于处理 GDK 事件。通过从GSource结构“派生”来创建新的源类型。源的派生类型由一个结构表示,该结构将GSource结构作为第一个元素,以及特定于新源类型的其他元素。要创建新源类型的实例,请调用g_source_new()并传入派生结构的大小和函数表。这些GSourceFunc确定新源类型的行为。

New source types basically interact with the main context in two ways. Their prepare function in GSourceFuncs can set a timeout to determine the maximum amount of time that the main loop will sleep before checking the source again. In addition, or as well, the source can add file descriptors to the set that the main context checks using g_source_add_poll().
新的源类型基本上以两种方式与主上下文交互。 GSourceFuncs中的准备函数可以设置超时,以确定主循环在再次检查源之前休眠的最长时间。此外,源还可以将文件描述符添加到主上下文使用g_source_add_poll()检查的集合中。

Customizing the main loop iteration
自定义主循环迭代

Single iterations of a GMainContext can be run with g_main_context_iteration(). In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the GMainLoop with an external main loop. In such cases, you can call the component functions of g_main_context_iteration() directly. These functions are g_main_context_prepare(), g_main_context_query(), g_main_context_check() and g_main_context_dispatch().
GMainContext的单次迭代可以使用g_main_context_iteration()运行。在某些情况下,需要对主循环的工作细节进行更详细的控制,例如,将GMainLoop与外部主循环集成时。在这种情况下,您可以直接调用g_main_context_iteration()的组件函数。这些函数是g_main_context_prepare()g_main_context_query()g_main_context_check()g_main_context_dispatch()

If the event loop thread releases GMainContext ownership until the results required by g_main_context_check() are ready you must create a context with the flag G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING or else you'll lose g_source_attach() notifications. This happens for instance when you integrate the GLib event loop into implementations that follow the proactor pattern (i.e. in these contexts the poll() implementation will reclaim the thread for other tasks until the results are ready). One example of the proactor pattern is the Boost.Asio library.
如果事件循环线程释放GMainContext所有权,直到g_main_context_check()所需的结果准备就绪,则必须使用标志创建一个上下文 G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING 否则您将丢失g_source_attach()通知。例如,当您将 GLib 事件循环集成到遵循前摄器模式的实现中时,就会发生这种情况(即,在这些上下文中, poll()实现将回收其他任务的线程,直到结果准备就绪)。前摄器模式的一个示例是 Boost.Asio 库。

State of a Main Context
主要上下文的状态

The operation of these functions can best be seen in terms of a state diagram, as shown in this image.
这些函数的操作可以通过状态图来最好地了解,如下图所示。

On UNIX, the GLib mainloop is incompatible with fork(). Any program using the mainloop must either exec() or exit() from the child without returning to the mainloop.
在 UNIX 上,GLib 主循环与fork()不兼容。任何使用主循环的程序都必须从子循环中执行exec()exit()而不返回到主循环。

Memory management of sources
源的内存管理

There are two options for memory management of the user data passed to a GSource to be passed to its callback on invocation. This data is provided in calls to g_timeout_add(), g_timeout_add_full(), g_idle_add(), etc. and more generally, using g_source_set_callback(). This data is typically an object which ‘owns’ the timeout or idle callback, such as a widget or a network protocol implementation. In many cases, it is an error for the callback to be invoked after this owning object has been destroyed, as that results in use of freed memory.
对于传递给GSource的用户数据进行内存管理有两个选项,以便在调用时传递给其回调。此数据在对g_timeout_add()g_timeout_add_full()g_idle_add()等的调用中提供,更一般地,使用g_source_set_callback()提供。该数据通常是“拥有”超时或空闲回调的对象,例如小部件或网络协议实现。在许多情况下,在拥有的对象被销毁后调用回调是错误的,因为这会导致使用已释放的内存。

The first, and preferred, option is to store the source ID returned by functions such as g_timeout_add() or g_source_attach(), and explicitly remove that source from the main context using g_source_remove() when the owning object is finalized. This ensures that the callback can only be invoked while the object is still alive.
第一个也是首选选项是存储g_timeout_add()g_source_attach()等函数返回的源 I​​D,并在所属对象完成时使用g_source_remove()从主上下文中显式删除该源。这确保了只能在对象仍然存在时调用回调。

The second option is to hold a strong reference to the object in the callback, and to release it in the callback’s GDestroyNotify. This ensures that the object is kept alive until after the source is finalized, which is guaranteed to be after it is invoked for the final time. The GDestroyNotify is another callback passed to the ‘full’ variants of GSource functions (for example, g_timeout_add_full()). It is called when the source is finalized, and is designed for releasing references like this.
第二个选项是在回调中保留对对象的强引用,并在回调的GDestroyNotify中释放它。这确保了对象在源完成之前一直保持活动状态,这保证是在最后一次调用它之后。 GDestroyNotify是传递给GSource函数的“完整”变体的另一个回调(例如, g_timeout_add_full() )。它在源代码最终确定时被调用,旨在释放此类引用。

One important caveat of this second approach is that it will keep the object alive indefinitely if the main loop is stopped before the GSource is invoked, which may be undesirable.
第二种方法的一个重要警告是,如果在调用GSource之前停止主循环,它将使对象无限期地保持活动状态,这可能是不可取的。

Functions 功能

g_main_loop_new () g_main_loop_new()

GMainLoop *
g_main_loop_new (GMainContext *context,
                 gboolean is_running);

Creates a new GMainLoop structure.
创建一个新的GMainLoop结构。

Parameters 参数

context 语境

a GMainContext (if NULL, the default context will be used).
GMainContext (如果NULL ,将使用默认上下文)。

[nullable]
[可为空]

is_running 正在运行

set to TRUE to indicate that the loop is running. This is not very important since calling g_main_loop_run() will set this to TRUE anyway.
设置为TRUE表示循环正在运行。这并不是很重要,因为调用g_main_loop_run()无论如何都会将其设置为TRUE

 

Returns 退货

a new GMainLoop.
一个新的GMainLoop


g_main_loop_ref ()

GMainLoop *
g_main_loop_ref (GMainLoop *loop);

Increases the reference count on a GMainLoop object by one.
GMainLoop对象的引用计数增加一。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

Returns 退货

loop


g_main_loop_unref ()

void
g_main_loop_unref (GMainLoop *loop);

Decreases the reference count on a GMainLoop object by one. If the result is zero, free the loop and free all associated memory.
GMainLoop对象的引用计数减一。如果结果为零,则释放循环并释放所有关联的内存。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

g_main_loop_run () g_main_loop_run()

void
g_main_loop_run (GMainLoop *loop);

Runs a main loop until g_main_loop_quit() is called on the loop. If this is called for the thread of the loop's GMainContext, it will process events from the loop, otherwise it will simply wait.
运行主循环,直到在循环中调用g_main_loop_quit() 。如果为循环的GMainContext线程调用此方法,它将处理循环中的事件,否则它将简单地等待。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

g_main_loop_quit () g_main_loop_quit()

void
g_main_loop_quit (GMainLoop *loop);

Stops a GMainLoop from running. Any calls to g_main_loop_run() for the loop will return.
停止GMainLoop的运行。任何对循环的g_main_loop_run()调用都将返回。

Note that sources that have already been dispatched when g_main_loop_quit() is called will still be executed.
请注意,调用g_main_loop_quit()时已分派的源仍将被执行。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

g_main_loop_is_running ()
g_main_loop_is_running()

gboolean
g_main_loop_is_running (GMainLoop *loop);

Checks to see if the main loop is currently being run via g_main_loop_run().
检查主循环当前是否正在通过g_main_loop_run()运行。

Parameters 参数

loop 环形

a GMainLoop.
一个GMainLoop

 

Returns 退货

TRUE if the mainloop is currently being run.
如果主循环当前正在运行, TRUE


g_main_loop_get_context ()
g_main_loop_get_context()

GMainContext *
g_main_loop_get_context (GMainLoop *loop);

Returns the GMainContext of loop .
返回loopGMainContext

Parameters 参数

loop 环形

a GMainLoop.
一个GMainLoop

 

Returns 退货

the GMainContext of loop .
loopGMainContext

[transfer none]
[无转移]


g_main_new()

#define             g_main_new(is_running)

g_main_new has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_new自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_loop_new() instead
使用g_main_loop_new()代替

Creates a new GMainLoop for th default main context.
为默认主上下文创建一个新的GMainLoop

Parameters 参数

is_running 正在运行

set to TRUE to indicate that the loop is running. This is not very important since calling g_main_run() will set this to TRUE anyway.
设置为TRUE表示循环正在运行。这并不是很重要,因为调用g_main_run()无论如何都会将其设置为TRUE

 

Returns 退货

a new GMainLoop
一个新的GMainLoop


g_main_destroy()

#define             g_main_destroy(loop)

g_main_destroy has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_destroy自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_loop_unref() instead
使用g_main_loop_unref()代替

Frees the memory allocated for the GMainLoop.
释放为GMainLoop分配的内存。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

g_main_run()

#define             g_main_run(loop)

g_main_run has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_run自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_loop_run() instead
使用g_main_loop_run()代替

Runs a main loop until it stops running.
运行主循环直到停止运行。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

g_main_quit()

#define             g_main_quit(loop)

g_main_quit has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_quit自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_loop_quit() instead
使用g_main_loop_quit()代替

Stops the GMainLoop. If g_main_run() was called to run the GMainLoop, it will now return.
停止GMainLoop 。如果调用g_main_run()来运行GMainLoop ,它现在将返回。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

g_main_is_running()

#define             g_main_is_running(loop)

g_main_is_running has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_is_running自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_loop_is_running() instead
使用g_main_loop_is_running()代替

Checks if the main loop is running.
检查主循环是否正在运行。

Parameters 参数

loop 环形

a GMainLoop
一个GMainLoop

 

Returns 退货

TRUE if the main loop is running
如果主循环正在运行TRUE


g_main_context_new () g_main_context_new()

GMainContext *
g_main_context_new (void);

Creates a new GMainContext structure.
创建一个新的GMainContext结构。

Returns 退货

the new GMainContext
新的GMainContext


g_main_context_new_with_flags ()

GMainContext *
g_main_context_new_with_flags (GMainContextFlags flags);

Creates a new GMainContext structure.
创建一个新的GMainContext结构。

Parameters 参数

flags 旗帜

a bitwise-OR combination of GMainContextFlags flags that can only be set at creation time.
GMainContextFlags标志的按位或组合,只能在创建时设置。

 

Returns 退货

the new GMainContext.
新的GMainContext

[transfer full]
[转满]

Since: 2.72 自: 2.72


g_main_context_ref ()

GMainContext *
g_main_context_ref (GMainContext *context);

Increases the reference count on a GMainContext object by one.
GMainContext对象的引用计数增加一。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

Returns 退货

the context that was passed in (since 2.6)
传入的context (自 2.6 起)


g_main_context_unref ()

void
g_main_context_unref (GMainContext *context);

Decreases the reference count on a GMainContext object by one. If the result is zero, free the context and free all associated memory.
GMainContext对象的引用计数减一。如果结果为零,则释放上下文并释放所有关联的内存。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

g_main_context_default ()
g_main_context_default()

GMainContext *
g_main_context_default (void);

Returns the global default main context. This is the main context used for main loop functions when a main loop is not explicitly specified, and corresponds to the "main" main loop. See also g_main_context_get_thread_default().
返回全局默认主上下文。这是当未显式指定主循环时用于主循环函数的主要上下文,并且对应于“main”主循环。参见 g_main_context_get_thread_default()

Returns 退货

the global default main context.
全局默认主上下文。

[transfer none]
[无转移]


g_main_context_iteration ()
g_main_context_iteration()

gboolean
g_main_context_iteration (GMainContext *context,
                          gboolean 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. Otherwise, if may_block is FALSE sources are not waited to become ready, only those highest priority events sources will be dispatched (if any), that are ready at this given moment without further waiting.
为给定的主循环运行一次迭代。这涉及检查是否有任何事件源准备好处理,如果没有事件源准备好并且may_blockTRUE ,则​​等待源准备好,然后分派准备就绪的最高优先级事件源。否则,如果may_blockFALSE则不会等待源准备就绪,只有那些最高优先级的事件源将被调度(如果有),这些事件源在给定时刻已准备好,无需进一步等待。

Note that even when may_block is TRUE, it is still possible for g_main_context_iteration() to return FALSE, since the wait may be interrupted for other reasons than an event source becoming ready.
请注意,即使may_blockTRUEg_main_context_iteration()仍然有可能返回FALSE ,因为等待可能会因事件源准备就绪以外的其他原因而中断。

Parameters 参数

context 语境

a GMainContext (if NULL, the default context will be used).
GMainContext (如果NULL ,将使用默认上下文)。

[nullable]
[可为空]

may_block 可能会阻止

whether the call may block.
调用是否可能阻塞。

 

Returns 退货

TRUE if events were dispatched.
如果事件已分派, TRUE


g_main_iteration()

#define             g_main_iteration(may_block)

g_main_iteration has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_iteration自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_context_iteration() instead.
使用g_main_context_iteration()代替。

Runs a single iteration for the default GMainContext.
为默认的GMainContext运行一次迭代。

Parameters 参数

may_block 可能会阻止

set to TRUE if it should block (i.e. wait) until an event source becomes ready. It will return after an event source has been processed. If set to FALSE it will return immediately if no event source is ready to be processed.
如果它应该阻塞(即等待)直到事件源准备好,则设置为TRUE 。处理事件源后它将返回。如果设置为FALSE如果没有事件源可供处理,它将立即返回。

 

Returns 退货

TRUE if more events are pending.
如果还有更多事件待处理, TRUE


g_main_context_pending ()
g_main_context_pending()

gboolean
g_main_context_pending (GMainContext *context);

Checks if any sources have pending events for the given context.
检查是否有任何源有给定上下文的待处理事件。

Parameters 参数

context 语境

a GMainContext (if NULL, the default context will be used).
GMainContext (如果NULL ,将使用默认上下文)。

[nullable]
[可为空]

Returns 退货

TRUE if events are pending.
如果事件待处理, TRUE


g_main_pending

#define             g_main_pending()

g_main_pending has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_pending自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_context_pending() instead.
使用g_main_context_pending()代替。

Checks if any events are pending for the default GMainContext (i.e. ready to be processed).
检查默认GMainContext是否有任何待处理事件(即准备好处理)。

Returns 退货

TRUE if any events are pending.
如果有任何事件待处理, TRUE


g_main_context_find_source_by_id ()

GSource *
g_main_context_find_source_by_id (GMainContext *context,
                                  guint source_id);

Finds a GSource given a pair of context and ID.
在给定一对上下文和 ID 的情况下查找GSource

It is a programmer error to attempt to look up a non-existent source.
尝试查找不存在的源是程序员的错误。

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with g_idle_add(): the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.
更具体地说:源 ID 可以在源被销毁后重新发布,因此使用此函数与可能已被删除的源 ID 永远是无效的。一个示例是,当使用g_idle_add()调度空闲在另一个线程中运行时:在其(现在无效)源 ID 上调用此函数时,空闲可能已经运行并被删除。该源 ID 可能已重新发布,从而导致针对错误的源执行操作。

Parameters 参数

context 语境

a GMainContext (if NULL, the default context will be used).
GMainContext (如果NULL ,将使用默认上下文)。

[nullable]
[可为空]

source_id 源ID

the source ID, as returned by g_source_get_id().
源 ID,由g_source_get_id()返回。

 

Returns 退货

the GSource.
G源

[transfer none]
[无转移]


g_main_context_find_source_by_user_data ()

GSource *
g_main_context_find_source_by_user_data
                               (GMainContext *context,
                                gpointer user_data);

Finds a source with the given user data for the callback. If multiple sources exist with the same user data, the first one found will be returned.
查找具有给定用户数据的回调源。如果存在具有相同用户数据的多个源,则将返回找到的第一个源。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

user_data 用户数据

the user_data for the callback.
回调的 user_data。

 

Returns 退货

the source, if one was found, otherwise NULL.
源(如果找到),否则为NULL

[transfer none]
[无转移]


g_main_context_find_source_by_funcs_user_data ()

GSource *
g_main_context_find_source_by_funcs_user_data
                               (GMainContext *context,
                                GSourceFuncs *funcs,
                                gpointer user_data);

Finds a source with the given source functions and user data. If multiple sources exist with the same source function and user data, the first one found will be returned.
查找具有给定源函数和用户数据的源。如果存在具有相同源函数和用户数据的多个源,则将返回找到的第一个源。

Parameters 参数

context 语境

a GMainContext (if NULL, the default context will be used).
GMainContext (如果NULL ,将使用默认上下文)。

[nullable]
[可为空]

funcs 函数

the source_funcs passed to g_source_new().
source_funcs传递给g_source_new()

 

user_data 用户数据

the user data from the callback.
来自回调的用户数据。

 

Returns 退货

the source, if one was found, otherwise NULL.
源(如果找到),否则为NULL

[transfer none]
[无转移]


g_main_context_wakeup () g_main_context_wakeup()

void
g_main_context_wakeup (GMainContext *context);

If context is currently blocking in g_main_context_iteration() waiting for a source to become ready, cause it to stop blocking and return. Otherwise, cause the next invocation of g_main_context_iteration() to return without blocking.
如果context当前在g_main_context_iteration()中阻塞等待源准备就绪,则使其停止阻塞并返回。否则,导致g_main_context_iteration()的下一次调用返回而不阻塞。

This API is useful for low-level control over GMainContext; for example, integrating it with main loop implementations such as GMainLoop.
此 API 对于GMainContext的低级控制很有用;例如,将其与GMainLoop等主循环实现集成。

Another related use for this function is when implementing a main loop with a termination condition, computed from multiple threads:
此函数的另一个相关用途是实现具有终止条件的主循环(从多个线程计算):

1
2
3
4
5
6
#define NUM_TASKS 10
static gint tasks_remaining = NUM_TASKS;  // (atomic)
...
 
while (g_atomic_int_get (&tasks_remaining) != 0)
  g_main_context_iteration (NULL, TRUE);

Then in a thread: 然后在一个线程中:

1
2
3
4
perform_work();

if (g_atomic_int_dec_and_test (&tasks_remaining))
  g_main_context_wakeup (NULL);

Parameters 参数

context 语境

a GMainContext
GMainContext

 

g_main_context_acquire ()
g_main_context_acquire()

gboolean
g_main_context_acquire (GMainContext *context);

Tries to become the owner of the specified context. If some other thread is the owner of the context, returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when g_main_context_release() is called as many times as g_main_context_acquire().
尝试成为指定上下文的所有者。如果某个其他线程是上下文的所有者,则立即返回FALSE 。所有权是正确递归的:所有者可以再次请求所有权,并在调用g_main_context_release()g_main_context_acquire()次数相同时释放所有权。

You must be the owner of a context before you can call g_main_context_prepare(), g_main_context_query(), g_main_context_check(), g_main_context_dispatch().
您必须是上下文的所有者,然后才能调用g_main_context_prepare()g_main_context_query()g_main_context_check()g_main_context_dispatch()

Parameters 参数

context 语境

a GMainContext
GMainContext

 

Returns 退货

TRUE if the operation succeeded, and this thread is now the owner of context .
如果操作成功, TRUE ,并且该线程现在是context的所有者。


g_main_context_release ()
g_main_context_release()

void
g_main_context_release (GMainContext *context);

Releases ownership of a context previously acquired by this thread with g_main_context_acquire(). If the context was acquired multiple times, the ownership will be released only when g_main_context_release() is called as many times as it was acquired.
释放该线程之前使用g_main_context_acquire()获取的上下文的所有权。如果上下文被多次获取,则只有当g_main_context_release()被调用的次数与获取的次数相同时,所有权才会被释放。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

g_main_context_is_owner ()

gboolean
g_main_context_is_owner (GMainContext *context);

Determines whether this thread holds the (recursive) ownership of this GMainContext. This is useful to know before waiting on another thread that may be blocking to get ownership of context .
确定此线程是否拥有此GMainContext的(递归)所有权。在等待可能阻塞的另一个线程获取context所有权之前了解这一点很有用。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

Returns 退货

TRUE if current thread is owner of context .
如果当前线程是context的所有者, TRUE

Since: 2.10 自: 2.10


g_main_context_wait () g_main_context_wait()

gboolean
g_main_context_wait (GMainContext *context,
                     GCond *cond,
                     GMutex *mutex);

g_main_context_wait has been deprecated since version 2.58 and should not be used in newly-written code.
g_main_context_wait自版本 2.58 起已被弃用,不应在新编写的代码中使用。

Use g_main_context_is_owner() and separate locking instead.
使用g_main_context_is_owner()并单独锁定。

Tries to become the owner of the specified context, as with g_main_context_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.
尝试成为指定上下文的所有者,如g_main_context_acquire() 。但是,如果另一个线程是所有者,则自动删除mutex并等待cond直到该所有者释放所有权或直到cond发出信号,然后再次尝试(一次)成为所有者。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

cond 条件

a condition variable 条件变量

 

mutex 互斥体

a mutex, currently held 当前持有的互斥锁

 

Returns 退货

TRUE if the operation succeeded, and this thread is now the owner of context .
如果操作成功, TRUE ,并且该线程现在是context的所有者。


g_main_context_prepare ()
g_main_context_prepare()

gboolean
g_main_context_prepare (GMainContext *context,
                        gint *priority);

Prepares to poll sources within a main loop. The resulting information for polling is determined by calling g_main_context_query().
准备在主循环内轮询源。轮询的结果信息是通过调用g_main_context_query()确定的。

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.
您必须先使用g_main_context_acquire()成功获取上下文,然后才能调用此函数。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

priority 优先事项

location to store priority of highest priority source already ready.
存储已准备就绪的最高优先级源的优先级的位置。

[out][optional]
[输出][可选]

Returns 退货

TRUE if some source is ready to be dispatched prior to polling.
如果某些源在轮询之前已准备好分派, TRUE


g_main_context_query () g_main_context_query()

gint
g_main_context_query (GMainContext *context,
                      gint max_priority,
                      gint *timeout_,
                      GPollFD *fds,
                      gint n_fds);

Determines information necessary to poll this main loop. You should be careful to pass the resulting fds array and its length n_fds as is when calling g_main_context_check(), as this function relies on assumptions made when the array is filled.
确定轮询该主循环所需的信息。在调用g_main_context_check()时,您应该小心地传递生成的fds数组及其长度n_fds ,因为此函数依赖于数组填充时所做的假设。

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.
您必须先使用g_main_context_acquire()成功获取上下文,然后才能调用此函数。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

max_priority 最大优先级

maximum priority source to check
要检查的最大优先级源

 

timeout_ 暂停_

location to store timeout to be used in polling.
存储轮询中使用的超时的位置。

[out]
[出去]

fds

location to store GPollFD records that need to be polled.
存储需要轮询的GPollFD记录的位置。

[out caller-allocates][array length=n_fds]
[调用者分配][数组长度=n_fds]

n_fds

length of fds .
fds的长度。

[in]
[]

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.
fds中实际存储的记录数,或者,如果需要存储超过n_fds记录,则需要存储的记录数。


g_main_context_check () g_main_context_check()

gboolean
g_main_context_check (GMainContext *context,
                      gint max_priority,
                      GPollFD *fds,
                      gint n_fds);

Passes the results of polling back to the main loop. You should be careful to pass fds and its length n_fds as received from g_main_context_query(), as this functions relies on assumptions on how fds is filled.
将轮询结果传递回主循环。您应该小心传递从g_main_context_query()接收到的fds及其长度n_fds ,因为此函数依赖于对fds填充方式的假设。

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.
您必须先使用g_main_context_acquire()成功获取上下文,然后才能调用此函数。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

max_priority 最大优先级

the maximum numerical priority of sources to check
要检查的源的最大数字优先级

 

fds

array of GPollFD's that was passed to the last call to g_main_context_query().
传递给最后一次调用g_main_context_query()GPollFD数组。

[array length=n_fds]
[数组长度=n_fds]

n_fds

return value of g_main_context_query()
g_main_context_query()的返回值

 

Returns 退货

TRUE if some sources are ready to be dispatched.
如果某些源已准备好发送, TRUE


g_main_context_dispatch ()
g_main_context_dispatch()

void
g_main_context_dispatch (GMainContext *context);

Dispatches all pending sources.
调度所有待处理的源。

You must have successfully acquired the context with g_main_context_acquire() before you may call this function.
您必须先使用g_main_context_acquire()成功获取上下文,然后才能调用此函数。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

g_main_context_set_poll_func ()

void
g_main_context_set_poll_func (GMainContext *context,
                              GPollFunc 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).
设置用于处理文件描述符轮询的函数。将使用它将代替poll()系统调用(或 GLib 的替换函数,该函数在poll()不可用的情况下使用)。

This function could possibly be used to integrate the GLib event loop with an external event loop.
该函数可用于将 GLib 事件循环与外部事件循环集成。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

func 功能

the function to call to poll all file descriptors
调用轮询所有文件描述符的函数

 

g_main_context_get_poll_func ()

GPollFunc
g_main_context_get_poll_func (GMainContext *context);

Gets the poll function set by g_main_context_set_poll_func().
获取由以下设置的 poll 函数 g_main_context_set_poll_func()

Parameters 参数

context 语境

a GMainContext
GMainContext

 

Returns 退货

the poll function 轮询函数


GPollFunc () GPollFunc()

gint
(*GPollFunc) (GPollFD *ufds,
              guint nfsd,
              gint timeout_);

Specifies the type of function passed to g_main_context_set_poll_func(). The semantics of the function should match those of the poll() system call.
指定传递给的函数类型 g_main_context_set_poll_func() 。该函数的语义应与poll()系统调用的语义相匹配。

Parameters 参数

ufds

an array of GPollFD elements
GPollFD元素数组

 

nfsd 国家食品安全标准

the number of elements in ufds
ufds中的元素数量

 

timeout_ 暂停_

the maximum time to wait for an event of the file descriptors. A negative value indicates an infinite timeout.
等待文件描述符事件的最长时间。负值表示无限超时。

 

Returns 退货

the number of GPollFD elements which have events or errors reported, or -1 if an error occurred.
报告了事件或错误的GPollFD元素的数量,如果发生错误则为 -1。


g_main_context_add_poll ()
g_main_context_add_poll()

void
g_main_context_add_poll (GMainContext *context,
                         GPollFD *fd,
                         gint priority);

Adds a file descriptor to the set of file descriptors polled for this context. This will very seldom be used directly. Instead a typical event source will use g_source_add_unix_fd() instead.
将文件描述符添加到为此上下文轮询的文件描述符集中。这很少会被直接使用。相反,典型的事件源将使用g_source_add_unix_fd()代替。

Parameters 参数

context 语境

a GMainContext (or NULL for the default context).
GMainContext (或默认上下文为NULL )。

[nullable]
[可为空]

fd

a GPollFD structure holding information about a file descriptor to watch.
一个GPollFD结构,保存有关要监视的文件描述符的信息。

 

priority 优先事项

the priority for this file descriptor which should be the same as the priority used for g_source_attach() to ensure that the file descriptor is polled whenever the results may be needed.
该文件描述符的优先级应与g_source_attach()使用的优先级相同,以确保每当需要结果时都会轮询文件描述符。

 

g_main_context_remove_poll ()

void
g_main_context_remove_poll (GMainContext *context,
                            GPollFD *fd);

Removes file descriptor from the set of file descriptors to be polled for a particular context.
从要轮询特定上下文的文件描述符集中删除文件描述符。

Parameters 参数

context 语境

a GMainContext
GMainContext

 

fd

a GPollFD descriptor previously added with g_main_context_add_poll()
先前使用g_main_context_add_poll()添加的GPollFD描述符

 

g_main_depth () g_main_深度()

gint
g_main_depth (void);

Returns the depth of the stack of calls to g_main_context_dispatch() on any GMainContext in the current thread. That is, when called from the toplevel, it gives 0. When called from within a callback from g_main_context_iteration() (or g_main_loop_run(), etc.) it returns 1. When called from within a callback to a recursive call to g_main_context_iteration(), it returns 2. And so forth.
返回当前线程中任何GMainContext上对g_main_context_dispatch()的调用堆栈的深度。也就是说,当从顶层调用时,它给出 0。当从g_main_context_iteration() (或g_main_loop_run()等)的回调中调用时,它返回 1。当从g_main_context_iteration()递归调用的回调中调用时,它返回 1。 ,它返回 2。依此类推。

This function is useful in a situation like the following: Imagine an extremely simple "garbage collected" system.
该函数在如下情况下非常有用:想象一个极其简单的“垃圾收集”系统。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static GList *free_list;

gpointer
allocate_memory (gsize size)
{ 
  gpointer result = g_malloc (size);
  free_list = g_list_prepend (free_list, result);
  return result;
}

void
free_allocated_memory (void)
{
  GList *l;
  for (l = free_list; l; l = l->next);
    g_free (l->data);
  g_list_free (free_list);
  free_list = NULL;
 }

[...]

while (TRUE); 
 {
   g_main_context_iteration (NULL, TRUE);
   free_allocated_memory();
  }

This works from an application, however, if you want to do the same thing from a library, it gets more difficult, since you no longer control the main loop. You might think you can simply use an idle function to make the call to free_allocated_memory(), but that doesn't work, since the idle function could be called from a recursive callback. This can be fixed by using g_main_depth()
这可以在应用程序中工作,但是,如果您想从库中执行相同的操作,则会变得更加困难,因为您不再控制主循环。您可能认为可以简单地使用空闲函数来调用free_allocated_memory() ,但这不起作用,因为可以从递归回调中调用空闲函数。这可以通过使用g_main_depth()来修复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
gpointer
allocate_memory (gsize size)
{ 
  FreeListBlock *block = g_new (FreeListBlock, 1);
  block->mem = g_malloc (size);
  block->depth = g_main_depth ();   
  free_list = g_list_prepend (free_list, block);
  return block->mem;
}

void
free_allocated_memory (void)
{
  GList *l;
  
  int depth = g_main_depth ();
  for (l = free_list; l; );
    {
      GList *next = l->next;
      FreeListBlock *block = l->data;
      if (block->depth > depth)
        {
          g_free (block->mem);
          g_free (block);
          free_list = g_list_delete_link (free_list, l);
        }
              
      l = next;
    }
  }

There is a temptation to use g_main_depth() to solve problems with reentrancy. For instance, while waiting for data to be received from the network in response to a menu item, the menu item might be selected again. It might seem that one could make the menu item's callback return immediately and do nothing if g_main_depth() returns a value greater than 1. However, this should be avoided since the user then sees selecting the menu item do nothing. Furthermore, you'll find yourself adding these checks all over your code, since there are doubtless many, many things that the user could do. Instead, you can use the following techniques:
人们倾向于使用g_main_depth()来解决可重入问题。例如,当等待从网络接收响应于菜单项的数据时,可以再次选择该菜单项。如果g_main_depth()返回大于 1 的值,似乎可以使菜单项的回调立即返回并且不执行任何操作。但是,应该避免这种情况,因为用户会看到选择菜单项不执行任何操作。此外,您会发现自己在代码中添加了这些检查,因为用户无疑可以做很多很多事情。相反,您可以使用以下技术:

  1. Use gtk_widget_set_sensitive() or modal dialogs to prevent the user from interacting with elements while the main loop is recursing.
    使用gtk_widget_set_sensitive()或模式对话框来防止用户在主循环递归时与元素交互。

  2. Avoid main loop recursion in situations where you can't handle arbitrary callbacks. Instead, structure your code so that you simply return to the main loop and then get called again when there is more work to do.
    在无法处理任意回调的情况下避免主循环递归。相反,构建您的代码,以便您只需返回主循环,然后在有更多工作要做时再次调用。

Returns 退货

The main loop recursion level in the current thread
当前线程中的主循环递归级别


g_main_current_source () g_main_current_source()

GSource *
g_main_current_source (void);

Returns the currently firing source for this thread.
返回该线程当前的触发源。

Returns 退货

The currently firing source or NULL.
当前触发源或NULL

[transfer none][nullable]
[不传输][可为空]

Since: 2.12 自: 2.12


g_main_set_poll_func()

#define             g_main_set_poll_func(func)

g_main_set_poll_func has been deprecated since version 2.2 and should not be used in newly-written code.
g_main_set_poll_func自 2.2 版本起已被弃用,不应在新编写的代码中使用。

Use g_main_context_set_poll_func() again 使用 g_main_context_set_poll_func() 再次

Sets the function to use for the handle polling of file descriptors for the default main context.
设置用于默认主上下文的文件描述符句柄轮询的函数。

Parameters 参数

func 功能

the function to call to poll all file descriptors
调用轮询所有文件描述符的函数

 

g_main_context_invoke () g_main_context_invoke()

void
g_main_context_invoke (GMainContext *context,
                       GSourceFunc function,
                       gpointer data);

Invokes a function in such a way that context is owned during the invocation of function .
以在调用function期间拥有context的方式调用函数。

If context is NULL then the global default main context — as returned by g_main_context_default() — is used.
如果contextNULL ,则使用g_main_context_default()返回的全局默认主上下文。

If context is owned by the current thread, function is called directly. Otherwise, if context is the thread-default main context of the current thread and g_main_context_acquire() succeeds, then function is called and g_main_context_release() is called afterwards.
如果context由当前线程拥有,则直接调用function 。否则,如果context是当前线程的线程默认主上下文,并且g_main_context_acquire()成功,则调用function ,然后调用g_main_context_release()

In any other case, an idle source is created to call function and that source is attached to context (presumably to be run in another thread). The idle source is attached with G_PRIORITY_DEFAULT priority. If you want a different priority, use g_main_context_invoke_full().
在任何其他情况下,都会创建一个空闲源来调用function ,并将该源附加到context (可能在另一个线程中运行)。空闲源附加G_PRIORITY_DEFAULT优先级。如果您想要不同的优先级,请使用g_main_context_invoke_full()

Note that, as with normal idle functions, function should probably return FALSE. If it returns TRUE, it will be continuously run in a loop (and may prevent this call from returning).
请注意,与正常的空闲函数一样, function可能应该返回FALSE 。如果它返回TRUE ,它将在循环中连续运行(并且可能会阻止此调用返回)。

Parameters 参数

context 语境

a GMainContext, or NULL.
GMainContextNULL

[nullable]
[可为空]

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

Since: 2.28 自: 2.28


g_main_context_invoke_full ()

void
g_main_context_invoke_full (GMainContext *context,
                            gint priority,
                            GSourceFunc function,
                            gpointer data,
                            GDestroyNotify notify);

Invokes a function in such a way that context is owned during the invocation of function .
以在调用function期间拥有context的方式调用函数。

This function is the same as g_main_context_invoke() except that it lets you specify the priority in case function ends up being scheduled as an idle and also lets you give a GDestroyNotify for data .
此函数与g_main_context_invoke()相同,只是它允许您指定优先级,以防function最终被安排为空闲,并且还允许您为data提供GDestroyNotify

notify should not assume that it is called from any particular thread or with any particular context acquired.
notify不应假设它是从任何特定线程调用的,或者是在获取任何特定上下文的情况下调用的。

Parameters 参数

context 语境

a GMainContext, or NULL.
GMainContextNULL

[nullable]
[可为空]

priority 优先事项

the priority at which to run function
运行function的优先级

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

notify 通知

a function to call when data is no longer in use, or NULL.
data不再使用时调用的函数,或NULL

[nullable]
[可为空]

Since: 2.28 自: 2.28


g_main_context_pusher_new ()

GMainContextPusher *
g_main_context_pusher_new (GMainContext *main_context);

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.
main_context推入当前线程的新线程默认主上下文,使用 g_main_context_push_thread_default() ,并返回一个新的GMainContextPusher 。使用g_main_context_pusher_free()弹出。使用 g_main_context_pop_thread_default() GMainContextPusher存在时,在main_context上可能会导致未定义的行为。

Using two GMainContextPushers in the same scope is not allowed, as it leads to an undefined pop order.
不允许在同一范围内使用两个GMainContextPushers ,因为这会导致未定义的弹出顺序。

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:
这旨在与g_autoptr()一起使用。请注意, g_autoptr()仅在使用 GCC 或 clang 时可用,因此以下示例仅适用于这些编译器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 参数

main_context 主上下文

a main context to push.
推动的主要背景。

[transfer none]
[无转移]

Returns 退货

a GMainContextPusher.
一个GMainContextPusher

[transfer full]
[转满]

Since: 2.64 自: 2.64


g_main_context_pusher_free ()

void
g_main_context_pusher_free (GMainContextPusher *pusher);

Pop pusher ’s main context as the thread default main context. See g_main_context_pusher_new() for details.
Pop pusher的主上下文作为线程默认主上下文。有关详细信息,请参阅g_main_context_pusher_new()

This will pop the GMainContext as the current thread-default main context, but will not call g_main_context_unref() on it.
这会将GMainContext弹出为当前线程默认主上下文,但不会对其调用g_main_context_unref()

Parameters 参数

pusher 推手

a GMainContextPusher.
一个GMainContextPusher

[transfer full]
[转满]

Since: 2.64 自: 2.64


g_main_context_get_thread_default ()
g_main_context_get_thread_default()

GMainContext *
g_main_context_get_thread_default (void);

Gets the thread-default GMainContext for this thread. Asynchronous operations that want to be able to be run in contexts other than the default one should call this method or g_main_context_ref_thread_default() to get a GMainContext to add their GSources to. (Note that even in single-threaded programs applications may sometimes want to temporarily push a non-default context, so it is not safe to assume that this will always return NULL if you are running in the default thread.)
获取此线程的线程默认GMainContext 。希望能够在默认上下文以外的上下文中运行的异步操作应调用此方法或 g_main_context_ref_thread_default() 获取GMainContext以将其GSource添加到其中。 (请注意,即使在单线程程序中,应用程序有时也可能想要临时推送非默认上下文,因此如果您在默认线程中运行,则假设这将始终返回NULL是不安全的。)

If you need to hold a reference on the context, use g_main_context_ref_thread_default() instead.
如果您需要保留上下文的引用,请使用 g_main_context_ref_thread_default() 反而。

Returns 退货

the thread-default GMainContext, or NULL if the thread-default context is the global default context.
线程默认GMainContext ,如果线程默认上下文是全局默认上下文,则为NULL

[transfer none][nullable]
[不传输][可为空]

Since: 2.22 自: 2.22


g_main_context_ref_thread_default ()

GMainContext *
g_main_context_ref_thread_default (void);

Gets the thread-default GMainContext for this thread, as with g_main_context_get_thread_default(), but also adds a reference to it with g_main_context_ref(). In addition, unlike g_main_context_get_thread_default(), if the thread-default context is the global default context, this will return that GMainContext (with a ref added to it) rather than returning NULL.
获取该线程的线程默认GMainContext ,如下所示 g_main_context_get_thread_default() ,而且还使用g_main_context_ref()添加对它的引用。另外,与 g_main_context_get_thread_default() ,如果线程默认上下文是全局默认上下文,这将返回GMainContext (添加了引用)而不是返回NULL

Returns 退货

the thread-default GMainContext. Unref with g_main_context_unref() when you are done with it.
线程默认的GMainContext 。完成后使用g_main_context_unref()取消引用。

[transfer full]
[转满]

Since: 2.32 自: 2.32


g_main_context_push_thread_default ()
g_main_context_push_thread_default()

void
g_main_context_push_thread_default (GMainContext *context);

Acquires context and sets it as the thread-default context for the current thread. This will cause certain asynchronous operations (such as most gio-based I/O) which are started in this thread to run under context and deliver their results to its main loop, rather than running under the global default context in the main thread. Note that calling this function changes the context returned by g_main_context_get_thread_default(), not the one returned by g_main_context_default(), so it does not affect the context used by functions like g_idle_add().
获取context并将其设置为当前线程的线程默认上下文。这将导致在此线程中启动的某些异步操作(例如大多数基于 gio 的 I/O)在context下运行并将其结果传递到其主循环,而不是在主线程中的全局默认上下文下运行。请注意,调用此函数会更改返回的上下文 g_main_context_get_thread_default() ,而不是g_main_context_default()返回的值,因此它不会影响g_idle_add()等函数使用的上下文。

Normally you would call this function shortly after creating a new thread, passing it a GMainContext which will be run by a GMainLoop in that thread, to set a new default context for all async operations in that thread. In this case you may not need to ever call g_main_context_pop_thread_default(), assuming you want the new GMainContext to be the default for the whole lifecycle of the thread.
通常,您会在创建新线程后不久调用此函数,并向其传递将由该线程中的GMainLoop运行的GMainContext ,以便为该线程中的所有异步操作设置新的默认上下文。在这种情况下,您可能不需要致电 g_main_context_pop_thread_default() ,假设您希望新的GMainContext成为线程整个生命周期的默认值。

If you don't have control over how the new thread was created (e.g. in the new thread isn't newly created, or if the thread life cycle is managed by a GThreadPool), it is always suggested to wrap the logic that needs to use the new GMainContext inside a g_main_context_push_thread_default() / g_main_context_pop_thread_default() pair, otherwise threads that are re-used will end up never explicitly releasing the GMainContext reference they hold.
如果您无法控制新线程的创建方式(例如,新线程不是新创建的,或者线程生命周期由GThreadPool管理),则始终建议包装需要的逻辑在 a 中使用新的GMainContext g_main_context_push_thread_default() / g_main_context_pop_thread_default() 配对,否则重用的线程最终将永远不会显式释放它们持有的GMainContext引用。

In some cases you may want to schedule a single operation in a non-default context, or temporarily use a non-default context in the main thread. In that case, you can wrap the call to the asynchronous operation inside a g_main_context_push_thread_default() / g_main_context_pop_thread_default() pair, but it is up to you to ensure that no other asynchronous operations accidentally get started while the non-default context is active.
在某些情况下,您可能希望在非默认上下文中安排单个操作,或者在主线程中临时使用非默认上下文。在这种情况下,您可以将对异步操作的调用包装在 g_main_context_push_thread_default() / g_main_context_pop_thread_default() 配对,但您需要确保在非默认上下文处于活动状态时不会意外启动其他异步操作。

Beware that libraries that predate this function may not correctly handle being used from a thread with a thread-default context. Eg, see g_file_supports_thread_contexts().
请注意,早于该函数的库可能无法正确处理具有线程默认上下文的线程的使用。例如,参见 g_file_supports_thread_contexts()

Parameters 参数

context 语境

a GMainContext, or NULL for the global default context.
GMainContext ,或NULL表示全局默认上下文。

[nullable]
[可为空]

Since: 2.22 自: 2.22


g_main_context_pop_thread_default ()
g_main_context_pop_thread_default()

void
g_main_context_pop_thread_default (GMainContext *context);

Pops context off the thread-default context stack (verifying that it was on the top of the stack).
context从线程默认上下文堆栈中弹出(验证它位于堆栈顶部)。

Parameters 参数

context 语境

a GMainContext object, or NULL.
GMainContext对象,或NULL

[nullable]
[可为空]

Since: 2.22 自: 2.22


g_timeout_source_new () g_timeout_source_new()

GSource *
g_timeout_source_new (guint interval);

Creates a new timeout source.
创建新的超时源。

The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()添加到 GMainContext 中。

The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time().
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()

Parameters 参数

interval 间隔

the timeout interval in milliseconds.
超时间隔(以毫秒为单位)。

 

Returns 退货

the newly-created timeout source
新创建的超时源


g_timeout_source_new_seconds ()

GSource *
g_timeout_source_new_seconds (guint interval);

Creates a new timeout source.
创建新的超时源。

The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()添加到 GMainContext 中。

The scheduling granularity/accuracy of this timeout source will be in seconds.
该超时源的调度粒度/精度将以秒为单位。

The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time().
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()

Parameters 参数

interval 间隔

the timeout interval in seconds
超时间隔(以秒为单位)

 

Returns 退货

the newly-created timeout source
新创建的超时源

Since: 2.14 自: 2.14


g_timeout_add () g_timeout_add()

guint
g_timeout_add (guint interval,
               GSourceFunc function,
               gpointer data);

Sets a function to be called at regular intervals, with the default priority, G_PRIORITY_DEFAULT.
设置定期调用的函数,使用默认优先级G_PRIORITY_DEFAULT

The given function is called repeatedly until it returns G_SOURCE_REMOVE or FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The first call to the function will be at the end of the first interval .
给定的function会被重复调用,直到返回G_SOURCE_REMOVEFALSE ,此时超时会自动销毁,并且该函数不会再次被调用。第一次调用该函数将在第一个interval结束时进行。

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).
请注意,由于其他事件源的处理,超时函数可能会延迟。因此,不应依赖它们来进行精确计时。每次调用超时函数后,都会根据当前时间和给定的时间间隔重新计算下一次超时的时间(它不会尝试“弥补”延迟中损失的时间)。

See memory management of sources for details on how to handle the return value and memory management of data .
有关如何处理返回值和data内存管理的详细信息,请参阅源的内存管理

If you want to have a timer in the "seconds" range and do not care about the exact time of the first call of the timer, use the g_timeout_add_seconds() function; this function allows for more optimizations and more efficient system power usage.
如果你想有一个“秒”范围内的计时器,并且不关心第一次调用计时器的确切时间,请使用g_timeout_add_seconds()函数;此功能可以实现更多优化和更有效的系统电源使用。

This internally creates a main loop source using g_timeout_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.
这在内部使用g_timeout_source_new()创建一个主循环源,并使用g_source_attach()将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。

It is safe to call this function from any thread.
从任何线程调用此函数都是安全的。

The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time().
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()

Parameters 参数

interval 间隔

the time between calls to the function, in milliseconds (1/1000ths of a second)
调用函数之间的时间,以毫秒为单位(1/1000 秒)

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。


g_timeout_add_full () g_timeout_add_full()

guint
g_timeout_add_full (gint priority,
                    guint interval,
                    GSourceFunc function,
                    gpointer data,
                    GDestroyNotify notify);

Sets a function to be called at regular intervals, with the given priority. The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again. The notify function is called when the timeout is destroyed. The first call to the function will be at the end of the first interval .
设置一个以给定优先级定期调用的函数。该函数会被重复调用,直到返回FALSE ,此时超时自动销毁,并且该函数不会再次被调用。当超时被销毁时,会调用notify函数。第一次调用该函数将在第一个interval结束时进行。

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).
请注意,由于其他事件源的处理,超时函数可能会延迟。因此,不应依赖它们来进行精确计时。每次调用超时函数后,都会根据当前时间和给定的时间间隔重新计算下一次超时的时间(它不会尝试“弥补”延迟中损失的时间)。

See memory management of sources for details on how to handle the return value and memory management of data .
有关如何处理返回值和data内存管理的详细信息,请参阅源的内存管理

This internally creates a main loop source using g_timeout_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.
这在内部使用g_timeout_source_new()创建一个主循环源,并使用g_source_attach()将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。

The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time().
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()

[rename-to g_timeout_add]
[重命名为g_timeout_add]

Parameters 参数

priority 优先事项

the priority of the timeout source. Typically this will be in the range between G_PRIORITY_DEFAULT and G_PRIORITY_HIGH.
超时源的优先级。通常,这将在G_PRIORITY_DEFAULTG_PRIORITY_HIGH之间的范围内。

 

interval 间隔

the time between calls to the function, in milliseconds (1/1000ths of a second)
调用函数之间的时间,以毫秒为单位(1/1000 秒)

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

notify 通知

function to call when the timeout is removed, or NULL.
超时被删除时调用的函数,或NULL

[nullable]
[可为空]

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。


g_timeout_add_seconds () g_timeout_add_seconds()

guint
g_timeout_add_seconds (guint interval,
                       GSourceFunc function,
                       gpointer data);

Sets a function to be called at regular intervals with the default priority, G_PRIORITY_DEFAULT.
设置一个以默认优先级G_PRIORITY_DEFAULT定期调用的函数。

The function is called repeatedly until it returns G_SOURCE_REMOVE or FALSE, at which point the timeout is automatically destroyed and the function will not be called again.
该函数会被重复调用,直到返回G_SOURCE_REMOVEFALSE ,此时超时自动销毁,该函数不会再次被调用。

This internally creates a main loop source using g_timeout_source_new_seconds() and attaches it to the main loop context using g_source_attach(). You can do these steps manually if you need greater control. Also see g_timeout_add_seconds_full().
这在内部创建了一个主循环源,使用 g_timeout_source_new_seconds() 并使用g_source_attach()将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。另请参阅g_timeout_add_seconds_full()

It is safe to call this function from any thread.
从任何线程调用此函数都是安全的。

Note that the first call of the timer may not be precise for timeouts of one second. If you need finer precision and have such a timeout, you may want to use g_timeout_add() instead.
请注意,计时器的第一次调用对于一秒的超时可能不精确。如果您需要更精细的精度并有这样的超时,您可能需要使用g_timeout_add()代替。

See memory management of sources for details on how to handle the return value and memory management of data .
有关如何处理返回值和data内存管理的详细信息,请参阅源的内存管理

The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time().
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()

Parameters 参数

interval 间隔

the time between calls to the function, in seconds
调用函数之间的时间(以秒为单位)

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。

Since: 2.14 自: 2.14


g_timeout_add_seconds_full ()

guint
g_timeout_add_seconds_full (gint priority,
                            guint interval,
                            GSourceFunc function,
                            gpointer data,
                            GDestroyNotify notify);

Sets a function to be called at regular intervals, with priority .
设置一个定期调用的函数, priority为 。

The function is called repeatedly until it returns G_SOURCE_REMOVE or FALSE, at which point the timeout is automatically destroyed and the function will not be called again.
该函数会被重复调用,直到返回G_SOURCE_REMOVEFALSE ,此时超时自动销毁,该函数不会再次被调用。

Unlike g_timeout_add(), this function operates at whole second granularity. The initial starting point of the timer is determined by the implementation and the implementation is expected to group multiple timers together so that they fire all at the same time. To allow this grouping, the interval to the first timer is rounded and can deviate up to one second from the specified interval. Subsequent timer iterations will generally run at the specified interval.
g_timeout_add()不同,该函数以整个秒粒度运行。计时器的初始起点由实现确定,并且预期实现将多个计时器分组在一起,以便它们同时触发。为了允许这种分组,第一个计时器的interval被舍入,并且可以与指定的时间间隔最多偏差一秒。后续的计时器迭代通常会按照指定的时间间隔运行。

Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval
请注意,由于其他事件源的处理,超时函数可能会延迟。因此,不应依赖它们来进行精确计时。每次调用超时函数后,都会根据当前时间和给定的interval重新计算下一次超时的时间

See memory management of sources for details on how to handle the return value and memory management of data .
有关如何处理返回值和data内存管理的详细信息,请参阅源的内存管理

If you want timing more precise than whole seconds, use g_timeout_add() instead.
如果您希望计时比整秒更精确,请改用g_timeout_add()

The grouping of timers to fire at the same time results in a more power and CPU efficient behavior so if your timer is in multiples of seconds and you don't require the first timer exactly one second from now, the use of g_timeout_add_seconds() is preferred over g_timeout_add().
同时触发的计时器分组会导致更省电和更高效的 CPU 行为,因此,如果您的计时器以秒为单位,并且您不需要从现在起正好一秒后使用第一个计时器,则使用g_timeout_add_seconds()是优于g_timeout_add()

This internally creates a main loop source using g_timeout_source_new_seconds() and attaches it to the main loop context using g_source_attach(). You can do these steps manually if you need greater control.
这在内部创建了一个主循环源,使用 g_timeout_source_new_seconds() 并使用g_source_attach()将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。

It is safe to call this function from any thread.
从任何线程调用此函数都是安全的。

The interval given is in terms of monotonic time, not wall clock time. See g_get_monotonic_time().
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()

[rename-to g_timeout_add_seconds]
[重命名为g_timeout_add_seconds]

Parameters 参数

priority 优先事项

the priority of the timeout source. Typically this will be in the range between G_PRIORITY_DEFAULT and G_PRIORITY_HIGH.
超时源的优先级。通常,这将在G_PRIORITY_DEFAULTG_PRIORITY_HIGH之间的范围内。

 

interval 间隔

the time between calls to the function, in seconds
调用函数之间的时间(以秒为单位)

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

notify 通知

function to call when the timeout is removed, or NULL.
超时被删除时调用的函数,或NULL

[nullable]
[可为空]

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。

Since: 2.14 自: 2.14


g_idle_source_new () g_idle_source_new()

GSource *
g_idle_source_new (void);

Creates a new idle source.
创建一个新的空闲源。

The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed. Note that the default priority for idle sources is G_PRIORITY_DEFAULT_IDLE, as compared to other sources which have a default priority of G_PRIORITY_DEFAULT.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()添加到 GMainContext 中。请注意,与默认优先级为G_PRIORITY_DEFAULT的其他源相比,空闲源的默认优先级为G_PRIORITY_DEFAULT_IDLE

Returns 退货

the newly-created idle source
新创建的空闲源


g_idle_add () g_idle_add()

guint
g_idle_add (GSourceFunc function,
            gpointer data);

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.
添加一个函数,只要默认主循环中没有待处理的更高优先级事件,就会调用该函数。该函数被赋予默认的空闲优先级G_PRIORITY_DEFAULT_IDLE 。如果函数返回FALSE它将自动从事件源列表中删除,并且不会再次调用。

See memory management of sources for details on how to handle the return value and memory management of data .
有关如何处理返回值和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.
这在内部使用g_idle_source_new()创建一个主循环源,并使用g_source_attach()将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。

Parameters 参数

function 功能

function to call  要调用的函数

 

data 数据

data to pass to function .
要传递给function数据。

 

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。


g_idle_add_full () g_idle_add_full()

guint
g_idle_add_full (gint priority,
                 GSourceFunc function,
                 gpointer data,
                 GDestroyNotify notify);

Adds a function to be called whenever there are no higher priority events pending.
添加一个在没有更高优先级事件待处理时调用的函数。

If the function returns G_SOURCE_REMOVE or FALSE it is automatically removed from the list of event sources and will not be called again.
如果函数返回G_SOURCE_REMOVEFALSE它将自动从事件源列表中删除,并且不会再次调用。

See memory management of sources for details on how to handle the return value and memory management of data .
有关如何处理返回值和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.
这在内部使用g_idle_source_new()创建一个主循环源,并使用g_source_attach()将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。

[rename-to g_idle_add]
[重命名为g_idle_add]

Parameters 参数

priority 优先事项

the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
空闲源的优先级。通常,这将在G_PRIORITY_DEFAULT_IDLEG_PRIORITY_HIGH_IDLE之间的范围内。

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

notify 通知

function to call when the idle is removed, or NULL.
当空闲被删除时调用的函数,或者NULL

[nullable]
[可为空]

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。


g_idle_remove_by_data () g_idle_remove_by_data()

gboolean
g_idle_remove_by_data (gpointer data);

Removes the idle function with the given data.
使用给定数据删除空闲函数。

Parameters 参数

data 数据

the data for the idle source's callback.
空闲源回调的数据。

 

Returns 退货

TRUE if an idle source was found and removed.
如果找到并删除了空闲源, TRUE


GChildWatchFunc ()

void
(*GChildWatchFunc) (GPid pid,
                    gint wait_status,
                    gpointer user_data);

Prototype of a GChildWatchSource callback, called when a child process has exited.
GChildWatchSource回调的原型,在子进程退出时调用。

To interpret wait_status , see the documentation for g_spawn_check_wait_status(). In particular, on Unix platforms, note that it is usually not equal to the integer passed to exit() or returned from main().
要解释wait_status ,请参阅g_spawn_check_wait_status()的文档。特别是,在 Unix 平台上,请注意它通常不等于传递给exit()或从main()返回的整数。

Parameters 参数

pid

the process id of the child process
子进程的进程id

 

wait_status 等待状态

Status information about the child process, encoded in a platform-specific manner
有关子进程的状态信息,以特定于平台的方式编码

 

user_data 用户数据

user data passed to g_child_watch_add()
传递给g_child_watch_add()的用户数据

 

g_child_watch_source_new ()

GSource *
g_child_watch_source_new (GPid pid);

Creates a new child_watch source.
创建一个新的 child_watch 源。

The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()添加到 GMainContext 中。

Note that child watch sources can only be used in conjunction with g_spawn... when the G_SPAWN_DO_NOT_REAP_CHILD flag is used.
请注意,当使用G_SPAWN_DO_NOT_REAP_CHILD标志时,子监视源只能与g_spawn...结合使用。

Note that on platforms where GPid must be explicitly closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source.
请注意,在必须显式关闭GPid的平台上(请参阅g_spawn_close_pid() ),当源仍处于活动状态时,不得关闭pid 。通常,您需要在源的回调函数中调用g_spawn_close_pid()

On POSIX platforms, the following restrictions apply to this API due to limitations in POSIX process interfaces:
在 POSIX 平台上,由于 POSIX 进程接口的限制,以下限制适用于此 API:

  • pid must be a child of this process
    pid必须是该进程的子进程

  • pid must be positive
    pid必须为正

  • the application must not call waitpid with a non-positive first argument, for instance in another thread
    应用程序不得使用非正第一个参数调用waitpid ,例如在另一个线程中

  • the application must not wait for pid to exit by any other mechanism, including waitpid(pid, ...) or a second child-watch source for the same pid
    应用程序不得通过任何其他机制等待pid退出,包括waitpid(pid, ...)或同一pid的第二个子监视源

  • the application must not ignore SIGCHLD
    应用程序不得忽略SIGCHLD

If any of those conditions are not met, this and related APIs will not work correctly. This can often be diagnosed via a GLib warning stating that ECHILD was received by waitpid.
如果不满足其中任何条件,此 API 和相关 API 将无法正常工作。这通常可以通过 GLib 警告来诊断,该警告指出ECHILD已被waitpid接收。

Calling waitpid for specific processes other than pid remains a valid thing to do.
为除pid之外的特定进程调用waitpid仍然是有效的做法。

Parameters 参数

pid

process to watch. On POSIX the positive pid of a child process. On Windows a handle for a process (which doesn't have to be a child).
过程来观看。在 POSIX 上,子进程的正 pid。在 Windows 上,进程的句柄(不一定是子进程)。

 

Returns 退货

the newly-created child watch source
新创建的儿童手表源

Since: 2.4 自: 2.4


g_child_watch_add () g_child_watch_add()

guint
g_child_watch_add (GPid pid,
                   GChildWatchFunc function,
                   gpointer data);

Sets a function to be called when the child indicated by pid exits, at a default priority, G_PRIORITY_DEFAULT.
设置一个函数,当pid指示的子进程退出时,以默认优先级G_PRIORITY_DEFAULT调用。

If you obtain pid from g_spawn_async() or g_spawn_async_with_pipes() you will need to pass G_SPAWN_DO_NOT_REAP_CHILD as flag to the spawn function for the child watching to work.
如果您从g_spawn_async()g_spawn_async_with_pipes()获取pid ,则需要将G_SPAWN_DO_NOT_REAP_CHILD作为标志传递给 spawn 函数,以便子进程监视工作。

Note that on platforms where GPid must be explicitly closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source.
请注意,在必须显式关闭GPid的平台上(请参阅g_spawn_close_pid() ),当源仍处于活动状态时,不得关闭pid 。通常,您需要在源的回调函数中调用g_spawn_close_pid()

GLib supports only a single callback per process id. On POSIX platforms, the same restrictions mentioned for g_child_watch_source_new() apply to this function.
GLib 仅支持每个进程 ID 一个回调。在 POSIX 平台上,针对g_child_watch_source_new()提到的相同限制也适用于该函数。

This internally creates a main loop source using g_child_watch_source_new() and attaches it to the main loop context using g_source_attach(). You can do these steps manually if you need greater control.
这在内部使用g_child_watch_source_new()创建一个主循环源,并使用g_source_attach()将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。

Parameters 参数

pid

process id to watch. On POSIX the positive pid of a child process. On Windows a handle for a process (which doesn't have to be a child).
要观看的进程 ID。在 POSIX 上,子进程的正 pid。在 Windows 上,进程的句柄(不一定是子进程)。

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。

Since: 2.4 自: 2.4


g_child_watch_add_full ()

guint
g_child_watch_add_full (gint priority,
                        GPid pid,
                        GChildWatchFunc function,
                        gpointer data,
                        GDestroyNotify notify);

Sets a function to be called when the child indicated by pid exits, at the priority priority .
设置当pid指示的子进程退出时要调用的函数, priority为priority。

If you obtain pid from g_spawn_async() or g_spawn_async_with_pipes() you will need to pass G_SPAWN_DO_NOT_REAP_CHILD as flag to the spawn function for the child watching to work.
如果您从g_spawn_async()g_spawn_async_with_pipes()获取pid ,则需要将G_SPAWN_DO_NOT_REAP_CHILD作为标志传递给 spawn 函数,以便子进程监视工作。

In many programs, you will want to call g_spawn_check_wait_status() in the callback to determine whether or not the child exited successfully.
在许多程序中,您需要在回调中调用g_spawn_check_wait_status()来确定子进程是否成功退出。

Also, note that on platforms where GPid must be explicitly closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you should invoke g_spawn_close_pid() in the callback function for the source.
另请注意,在必须显式关闭GPid 的平台上(请参阅g_spawn_close_pid() ),当源仍处于活动状态时,不得关闭pid 。通常,您应该在源的回调函数中调用g_spawn_close_pid()

GLib supports only a single callback per process id. On POSIX platforms, the same restrictions mentioned for g_child_watch_source_new() apply to this function.
GLib 仅支持每个进程 ID 一个回调。在 POSIX 平台上,针对g_child_watch_source_new()提到的相同限制也适用于该函数。

This internally creates a main loop source using g_child_watch_source_new() and attaches it to the main loop context using g_source_attach(). You can do these steps manually if you need greater control.
这在内部使用g_child_watch_source_new()创建一个主循环源,并使用g_source_attach()将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。

[rename-to g_child_watch_add]
[重命名为g_child_watch_add]

Parameters 参数

priority 优先事项

the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE.
空闲源的优先级。通常,这将在G_PRIORITY_DEFAULT_IDLEG_PRIORITY_HIGH_IDLE之间的范围内。

 

pid

process to watch. On POSIX the positive pid of a child process. On Windows a handle for a process (which doesn't have to be a child).
过程来观看。在 POSIX 上,子进程的正 pid。在 Windows 上,进程的句柄(不一定是子进程)。

 

function 功能

function to call 要调用的函数

 

data 数据

data to pass to function
传递给function数据

 

notify 通知

function to call when the idle is removed, or NULL.
当空闲被删除时调用的函数,或者NULL

[nullable]
[可为空]

Returns 退货

the ID (greater than 0) of the event source.
事件源的ID(大于0)。

Since: 2.4 自: 2.4


g_poll () g_poll()

gint
g_poll (GPollFD *fds,
        guint nfds,
        gint timeout);

Polls fds , as with the poll() system call, but portably. (On systems that don't have poll(), it is emulated using select().) This is used internally by GMainContext, but it can be called directly if you need to block until a file descriptor is ready, but don't want to run the full main loop.
轮询fds ,与poll()系统调用一样,但可移植。 (在没有poll()系统上,它是使用select()模拟的。)这是由GMainContext内部使用的,但如果您需要阻塞直到文件描述符准备好,则可以直接调用它,但不要想要运行完整的主循环。

Each element of fds is a GPollFD describing a single file descriptor to poll. The fd field indicates the file descriptor, and the events field indicates the events to poll for. On return, the revents fields will be filled with the events that actually occurred.
fds的每个元素都是一个GPollFD,描述要轮询的单个文件描述符。 fd字段指示文件描述符, events字段指示要轮询的事件。返回时, revents字段将填充实际发生的事件。

On POSIX systems, the file descriptors in fds can be any sort of file descriptor, but the situation is much more complicated on Windows. If you need to use g_poll() in code that has to run on Windows, the easiest solution is to construct all of your GPollFDs with g_io_channel_win32_make_pollfd().
在 POSIX 系统上, fds中的文件描述符可以是任何类型的文件描述符,但在 Windows 上情况要复杂得多。如果您需要在必须在 Windows 上运行的代码中使用g_poll() ,最简单的解决方案是使用以下命令构建所有GPollFDg_io_channel_win32_make_pollfd()

Parameters 参数

fds

file descriptors to poll 要轮询的文件描述符

 

nfds 国家食品药品监督管理局

the number of file descriptors in fds
fds中文件描述符的数量

 

timeout 暂停

amount of time to wait, in milliseconds, or -1 to wait forever
等待的时间量(以毫秒为单位),或 -1 表示永远等待

 

Returns 退货

the number of entries in fds whose revents fields were filled in, or 0 if the operation timed out, or -1 on error or if the call was interrupted.
fds中已填写revents字段的条目数,如果操作超时则为 0,如果出错或调用被中断则为 -1。

Since: 2.20 自: 2.20


GSourceDummyMarshal ()

void
(*GSourceDummyMarshal) (void);

This is just a placeholder for GClosureMarshal, which cannot be used here for dependency reasons.
这只是GCloseMarshal的占位符,由于依赖性原因不能在此处使用。


GSourceDisposeFunc () GSourceDisposeFunc()

void
(*GSourceDisposeFunc) (GSource *source);

Dispose function for source . See g_source_set_dispose_function() for details.
source的处置函数。看 g_source_set_dispose_function() 了解详情。

Parameters 参数

source 来源

GSource that is currently being disposed
当前正在处置的GSource

 

Since: 2.64 自: 2.64


g_source_new () g_source_new()

GSource *
g_source_new (GSourceFuncs *source_funcs,
              guint struct_size);

Creates a new GSource structure. The size is specified to allow creating structures derived from GSource that contain additional data. The size passed in must be at least sizeof (GSource).
创建一个新的GSource结构。指定大小是为了允许创建从包含附加数据的GSource派生的结构。传入的大小必须至少为sizeof (GSource)

The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()添加到 GMainContext 中。

Parameters 参数

source_funcs 源函数

structure containing functions that implement the sources behavior.
包含实现源行为的函数的结构。

 

struct_size 结构体大小

size of the GSource structure to create.
要创建的GSource结构的大小。

 

Returns 退货

the newly-created GSource.
新创建的GSource


g_source_ref ()

GSource *
g_source_ref (GSource *source);

Increases the reference count on a source by one.
将源的引用计数增加一。

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

source


g_source_unref ()

void
g_source_unref (GSource *source);

Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.
将源的引用计数减一。如果生成的引用计数为零,则源和关联的内存将被破坏。

Parameters 参数

source 来源

a GSource
一个G源

 

g_source_set_funcs () g_source_set_funcs()

void
g_source_set_funcs (GSource *source,
                    GSourceFuncs *funcs);

Sets the source functions (can be used to override default implementations) of an unattached source.
设置未附加源的源函数(可用于覆盖默认实现)。

Parameters 参数

source 来源

a GSource
一个G源

 

funcs 函数

the new GSourceFuncs
新的GSourceFuncs

 

Since: 2.12 自: 2.12


g_source_set_dispose_function ()

void
g_source_set_dispose_function (GSource *source,
                               GSourceDisposeFunc dispose);

Set dispose as dispose function on source . dispose will be called once the reference count of source reaches 0 but before any of the state of the source is freed, especially before the finalize function is called.
dispose设置为source上的 dispose 函数。一旦source的引用计数达到0,但在释放source的任何状态之前,特别是在调用finalize函数之前,将调用dispose

This means that at this point source is still a valid GSource and it is allow for the reference count to increase again until dispose returns.
这意味着此时source仍然是有效的GSource ,并且允许引用计数再次增加,直到dispose返回。

The dispose function can be used to clear any "weak" references to the source in other data structures in a thread-safe way where it is possible for another thread to increase the reference count of source again while it is being freed.
dispose 函数可用于以线程安全的方式清除其他数据结构中对source的任何“弱”引用,其中另一个线程可以在释放源时再次增加source的引用计数。

The finalize function can not be used for this purpose as at that point source is already partially freed and not valid anymore.
Finalize 函数不能用于此目的,因为此时source已经部分释放并且不再有效。

This should only ever be called from GSource implementations.
这只应该从GSource实现中调用。

Parameters 参数

source 来源

A GSource to set the dispose function on
用于设置 dispose 函数的GSource

 

dispose 处置

GSourceDisposeFunc to set on the source
GSourceDisposeFunc在源上设置

 

Since: 2.64 自: 2.64


g_source_attach () g_source_attach()

guint
g_source_attach (GSource *source,
                 GMainContext *context);

Adds a GSource to a context so that it will be executed within that context. Remove it by calling g_source_destroy().
GSource添加到context以便它将在该上下文中执行。通过调用g_source_destroy()将其删除。

This function is safe to call from any thread, regardless of which thread the context is running in.
无论context在哪个线程中运行,都可以从任何线程安全地调用此函数。

Parameters 参数

source 来源

a GSource
一个G源

 

context 语境

a GMainContext (if NULL, the default context will be used).
GMainContext (如果NULL ,将使用默认上下文)。

[nullable]
[可为空]

Returns 退货

the ID (greater than 0) for the source within the GMainContext.
GMainContext中源的 ID(大于 0)。


g_source_destroy () g_source_destroy()

void
g_source_destroy (GSource *source);

Removes a source from its GMainContext, if any, and mark it as destroyed. The source cannot be subsequently added to another context. It is safe to call this on sources which have already been removed from their context.
从其GMainContext中删除源(如果有),并将其标记为已销毁。随后无法将源添加到另一个上下文中。在已经从上下文中删除的源上调用此方法是安全的。

This does not unref the GSource: if you still hold a reference, use g_source_unref() to drop it.
这不会取消引用GSource :如果您仍然持有引用,请使用g_source_unref()删除它。

This function is safe to call from any thread, regardless of which thread the GMainContext is running in.
无论GMainContext在哪个线程中运行,都可以从任何线程安全地调用此函数。

If the source is currently attached to a GMainContext, destroying it will effectively unset the callback similar to calling g_source_set_callback(). This can mean, that the data's GDestroyNotify gets called right away.
如果源当前附加到GMainContext ,则销毁它将有效地取消设置回调,类似于调用g_source_set_callback() 。这可能意味着数据的GDestroyNotify会立即被调用。

Parameters 参数

source 来源

a GSource
一个G源

 

g_source_is_destroyed ()

gboolean
g_source_is_destroyed (GSource *source);

Returns whether source has been destroyed.
返回source是否已被销毁。

This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.
当您从空闲处理程序中操作对象时,这一点很重要,但可能在调度空闲处理程序之前释放了对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
static gboolean 
idle_callback (gpointer data)
{
  SomeWidget *self = data;
   
  g_mutex_lock (&self->idle_id_mutex);
  // do stuff with self
  self->idle_id = 0;
  g_mutex_unlock (&self->idle_id_mutex);
   
  return G_SOURCE_REMOVE;
}
 
static void 
some_widget_do_stuff_later (SomeWidget *self)
{
  g_mutex_lock (&self->idle_id_mutex);
  self->idle_id = g_idle_add (idle_callback, self);
  g_mutex_unlock (&self->idle_id_mutex);
}
 
static void
some_widget_init (SomeWidget *self)
{
  g_mutex_init (&self->idle_id_mutex);

  // ...
}

static void 
some_widget_finalize (GObject *object)
{
  SomeWidget *self = SOME_WIDGET (object);
   
  if (self->idle_id)
    g_source_remove (self->idle_id);
   
  g_mutex_clear (&self->idle_id_mutex);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.
如果在空闲处理程序触发之前小部件由于在回调中使用 after free 而被销毁,则在多线程应用程序中这将失败。针对这个特定问题的解决方案是检查源是否已在回调中被销毁。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static gboolean 
idle_callback (gpointer data)
{
  SomeWidget *self = data;
  
  g_mutex_lock (&self->idle_id_mutex);
  if (!g_source_is_destroyed (g_main_current_source ()))
    {
      // do stuff with self
    }
  g_mutex_unlock (&self->idle_id_mutex);
  
  return FALSE;
}

Calls to this function from a thread other than the one acquired by the GMainContext the GSource is attached to are typically redundant, as the source could be destroyed immediately after this function returns. However, once a source is destroyed it cannot be un-destroyed, so this function can be used for opportunistic checks from any thread.
从附加GSourceGMainContext获取的线程以外的线程调用此函数通常是多余的,因为此函数返回后源可能会立即被销毁。然而,一旦源被销毁,它就无法恢复,因此该函数可用于任何线程的机会性检查。

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

TRUE if the source has been destroyed
如果源已被破坏TRUE

Since: 2.12 自: 2.12


g_source_set_priority () g_source_set_priority()

void
g_source_set_priority (GSource *source,
                       gint priority);

Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.
设置源的优先级。当主循环运行时,如果源已准备好被调度,并且没有更高(数字较小)优先级的源准备好被调度,则该源将被调度。

A child source always has the same priority as its parent. It is not permitted to change the priority of a source once it has been added as a child of another source.
子源始终与其父源具有相同的优先级。一旦将某个源添加为另一个源的子级,就不允许更改该源的优先级。

Parameters 参数

source 来源

a GSource
一个G源

 

priority 优先事项

the new priority. 新的优先事项。

 

g_source_get_priority () g_source_get_priority()

gint
g_source_get_priority (GSource *source);

Gets the priority of a source.
获取源的优先级。

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

the priority of the source
源的优先级


g_source_set_can_recurse ()
g_source_set_can_recurse()

void
g_source_set_can_recurse (GSource *source,
                          gboolean can_recurse);

Sets whether a source can be called recursively. If can_recurse is TRUE, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.
设置是否可以递归调用源。如果can_recurseTRUE ,则​​在调度源时,该源将被正常处理。否则,该源的所有处理都会被阻止,直到调度函数返回。

Parameters 参数

source 来源

a GSource
一个G源

 

can_recurse 可以递归

whether recursion is allowed for this source
该源是否允许递归

 

g_source_get_can_recurse ()
g_source_get_can_recurse()

gboolean
g_source_get_can_recurse (GSource *source);

Checks whether a source is allowed to be called recursively. see g_source_set_can_recurse().
检查是否允许递归调用源。请参阅g_source_set_can_recurse()

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

whether recursion is allowed.
是否允许递归。


g_source_get_id () g_source_get_id()

guint
g_source_get_id (GSource *source);

Returns the numeric ID for a particular source. The ID of a source is a positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by g_main_context_find_source_by_id().
返回特定源的数字 ID。源的 ID 是一个正整数,在特定的主循环上下文中是唯一的。从 ID 到源的反向映射是通过以下方式完成的 g_main_context_find_source_by_id()

You can only call this function while the source is associated to a GMainContext instance; calling this function before g_source_attach() or after g_source_destroy() yields undefined behavior. The ID returned is unique within the GMainContext instance passed to g_source_attach().
仅当源与GMainContext实例关联时才能调用此函数;在g_source_attach()之前或g_source_destroy()之后调用此函数会产生未定义的行为。返回的 ID 在传递给g_source_attach() GMainContext实例中是唯一的。

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

the ID (greater than 0) for the source
源的 ID(大于 0)


g_source_get_name ()

const char *
g_source_get_name (GSource *source);

Gets a name for the source, used in debugging and profiling. The name may be NULL if it has never been set with g_source_set_name().
获取源的名称,用于调试和分析。如果从未使用g_source_set_name()设置该名称,则该名称可能为NULL

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

the name of the source.
来源的名称。

[nullable]
[可为空]

Since: 2.26 自: 2.26


g_source_set_name ()

void
g_source_set_name (GSource *source,
                   const char *name);

Sets a name for the source, used in debugging and profiling. The name defaults to NULL.
设置源的名称,用于调试和分析。该名称默认为NULL

The source name should describe in a human-readable way what the source does. For example, "X11 event queue" or "GTK+ repaint idle handler" or whatever it is.
源名称应该以人类可读的方式描述源的用途。例如,“X11 事件队列”或“GTK+ 重绘空闲处理程序”或其他任何内容。

It is permitted to call this function multiple times, but is not recommended due to the potential performance impact. For example, one could change the name in the "check" function of a GSourceFuncs to include details like the event type in the source name.
允许多次调用此函数,但由于潜在的性能影响,不建议这样做。例如,可以更改GSourceFuncs的“检查”函数中的名称,以包含源名称中的事件类型等详细信息。

Use caution if changing the name while another thread may be accessing it with g_source_get_name(); that function does not copy the value, and changing the value will free it while the other thread may be attempting to use it.
如果在另一个线程可能正在使用g_source_get_name()访问该名称时更改名称,请务必小心;该函数不会复制该值,并且更改该值将释放该值,而其他线程可能会尝试使用该值。

Also see g_source_set_static_name().
另请参阅g_source_set_static_name()

Parameters 参数

source 来源

a GSource
一个G源

 

name 姓名

debug name for the source
源的调试名称

 

Since: 2.26 自: 2.26


g_source_set_static_name ()

void
g_source_set_static_name (GSource *source,
                          const char *name);

A variant of g_source_set_name() that does not duplicate the name , and can only be used with string literals.
g_source_set_name()的变体,不重复name ,并且只能与字符串文字一起使用。

Parameters 参数

source 来源

a GSource
一个G源

 

name 姓名

debug name for the source
源的调试名称

 

Since: 2.70 自: 2.70


g_source_set_name_by_id ()

void
g_source_set_name_by_id (guint tag,
                         const char *name);

Sets the name of a source using its ID.
使用源 ID 设置源的名称。

This is a convenience utility to set source names from the return value of g_idle_add(), g_timeout_add(), etc.
这是一个方便的实用程序,用于根据g_idle_add()g_timeout_add()等的返回值设置源名称。

It is a programmer error to attempt to set the name of a non-existent source.
尝试设置不存在的源的名称是程序员的错误。

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with g_idle_add(): the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.
更具体地说:源 ID 可以在源被销毁后重新发布,因此使用此函数与可能已被删除的源 ID 永远是无效的。一个示例是,当使用g_idle_add()调度空闲在另一个线程中运行时:在其(现在无效)源 ID 上调用此函数时,空闲可能已经运行并被删除。该源 ID 可能已重新发布,从而导致针对错误的源执行操作。

Parameters 参数

tag 标签

a GSource ID
G源ID

 

name 姓名

debug name for the source
源的调试名称

 

Since: 2.26 自: 2.26


g_source_get_context () g_source_get_context()

GMainContext *
g_source_get_context (GSource *source);

Gets the GMainContext with which the source is associated.
获取与源关联的GMainContext

You can call this on a source that has been destroyed, provided that the GMainContext it was attached to still exists (in which case it will return that GMainContext). In particular, you can always call this function on the source returned from g_main_current_source(). But calling this function on a source whose GMainContext has been destroyed is an error.
您可以在已销毁的源上调用此方法,前提是它所附加的GMainContext仍然存在(在这种情况下它将返回该GMainContext )。特别是,您始终可以在g_main_current_source()返回的源上调用此函数。但是在GMainContext已被破坏的源上调用此函数会出错。

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source.
与源关联的GMainContext ,如果上下文尚未添加到源,则为NULL

[transfer none][nullable]
[不传输][可为空]


g_source_set_callback () g_source_set_callback()

void
g_source_set_callback (GSource *source,
                       GSourceFunc func,
                       gpointer data,
                       GDestroyNotify notify);

Sets the callback function for a source. The callback for a source is called from the source's dispatch function.
设置源的回调函数。源的回调是从源的调度函数调用的。

The exact type of func depends on the type of source; ie. you should not count on func being called with data as its first parameter. Cast func with G_SOURCE_FUNC() to avoid warnings about incompatible function types.
func的确切类型取决于源的类型; IE。您不应该指望以data作为第一个参数来调用func 。使用G_SOURCE_FUNC()强制转换func以避免出现有关不兼容函数类型的警告。

See memory management of sources for details on how to handle memory management of data .
有关如何处理data内存管理的详细信息,请参阅源内存管理

Typically, you won't use this function. Instead use functions specific to the type of source you are using, such as g_idle_add() or g_timeout_add().
通常,您不会使用此功能。相反,请使用特定于您所使用的源类型的函数,例如g_idle_add()g_timeout_add()

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.
在已附加到上下文的源上多次调用此函数是安全的。更改将在此调用返回后下次分派源时生效。

Note that g_source_destroy() for a currently attached source has the effect of also unsetting the callback.
请注意,当前附加源的g_source_destroy()还具有取消设置回调的效果。

Parameters 参数

source 来源

the source 来源

 

func 功能

a callback function 回调函数

 

data 数据

the data to pass to callback function
传递给回调函数的数据

 

notify 通知

a function to call when data is no longer in use, or NULL.
data不再使用时调用的函数,或NULL

[nullable]
[可为空]

GSourceFunc () GSourceFunc()

gboolean
(*GSourceFunc) (gpointer user_data);

Specifies the type of function passed to g_timeout_add(), g_timeout_add_full(), g_idle_add(), and g_idle_add_full().
指定传递给g_timeout_add()g_timeout_add_full()g_idle_add()g_idle_add_full()的函数类型。

When calling g_source_set_callback(), you may need to cast a function of a different type to this type. Use G_SOURCE_FUNC() to avoid warnings about incompatible function types.
当调用g_source_set_callback()时,您可能需要将不同类型的函数转换为该类型。使用G_SOURCE_FUNC()以避免有关不兼容函数类型的警告。

Parameters 参数

user_data 用户数据

data passed to the function, set when the source was created with one of the above functions
传递给函数的数据,在使用上述函数之一创建源时设置

 

Returns 退货

FALSE if the source should be removed. G_SOURCE_CONTINUE and G_SOURCE_REMOVE are more memorable names for the return value.
如果应删除源, FALSEG_SOURCE_CONTINUEG_SOURCE_REMOVE是返回值的更容易记住的名称。


G_SOURCE_FUNC()

#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) GLIB_AVAILABLE_MACRO_IN_2_58

Cast a function pointer to a GSourceFunc, suppressing warnings from GCC 8 onwards with -Wextra or -Wcast-function-type enabled about the function types being incompatible.
将函数指针转换为GSourceFunc ,从 GCC 8 开始,通过启用-Wextra-Wcast-function-type来抑制有关函数类型不兼容的警告。

For example, the correct type of callback for a source created by g_child_watch_source_new() is GChildWatchFunc, which accepts more arguments than GSourceFunc. Casting the function with (GSourceFunc) to call g_source_set_callback() will trigger a warning, even though it will be cast back to the correct type before it is called by the source.
例如,由g_child_watch_source_new()创建的源的正确回调类型是GChildWatchFunc ,它比GSourceFunc接受更多的参数。使用(GSourceFunc)转换函数来调用g_source_set_callback()将触发警告,即使它在被源调用之前会被转换回正确的类型。

Parameters 参数

f

a function pointer. 函数指针。

 

Since: 2.58 自: 2.58


g_source_set_callback_indirect ()
g_source_set_callback_indirect()

void
g_source_set_callback_indirect (GSource *source,
                                gpointer callback_data,
                                GSourceCallbackFuncs *callback_funcs);

Sets the callback function storing the data as a refcounted callback "object". This is used internally. Note that calling g_source_set_callback_indirect() assumes an initial reference count on callback_data , and thus callback_funcs->unref will eventually be called once more than callback_funcs->ref .
设置将数据存储为引用计数回调“对象”的回调函数。这是内部使用的。请注意,调用 g_source_set_callback_indirect() 假设callback_data上有一个初始引用计数,因此callback_funcs->unref最终将比callback_funcs->ref被调用一次。

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.
在已附加到上下文的源上多次调用此函数是安全的。更改将在此调用返回后下次分派源时生效。

Parameters 参数

source 来源

the source 来源

 

callback_data 回调数据

pointer to callback data "object"
指向回调数据“对象”的指针

 

callback_funcs 回调函数

functions for reference counting callback_data and getting the callback and data
用于引用计数callback_data并获取回调和数据的函数

 

g_source_set_ready_time ()
g_source_set_ready_time()

void
g_source_set_ready_time (GSource *source,
                         gint64 ready_time);

Sets a GSource to be dispatched when the given monotonic time is reached (or passed). If the monotonic time is in the past (as it always will be if ready_time is 0) then the source will be dispatched immediately.
设置当达到(或超过)给定的单调时间时要调度的GSource 。如果单调时间已经过去(如果ready_time为0,则总是如此),则将立即分派源。

If ready_time is -1 then the source is never woken up on the basis of the passage of time.
如果ready_time为-1,则源永远不会根据时间的流逝而被唤醒。

Dispatching the source does not reset the ready time. You should do so yourself, from the source dispatch function.
调度源不会重置就绪时间。您应该从源调度函数中自己执行此操作。

Note that if you have a pair of sources where the ready time of one suggests that it will be delivered first but the priority for the other suggests that it would be delivered first, and the ready time for both sources is reached during the same main context iteration, then the order of dispatch is undefined.
请注意,如果您有一对源,其中一个源的就绪时间表明它将首先交付,但另一个源的优先级表明它将首先交付,并且两个源的就绪时间都是在同一主上下文期间达到的迭代,则调度顺序未定义。

It is a no-op to call this function on a GSource which has already been destroyed with g_source_destroy().
在已使用g_source_destroy()销毁的GSource上调用此函数是无操作的。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

Parameters 参数

source 来源

a GSource
一个G源

 

ready_time 准备时间

the monotonic time at which the source will be ready, 0 for "immediately", -1 for "never"
源准备就绪的单调时间,0 表示“立即”,-1 表示“从不”

 

Since: 2.36 自: 2.36


g_source_get_ready_time ()
g_source_get_ready_time()

gint64
g_source_get_ready_time (GSource *source);

Gets the "ready time" of source , as set by g_source_set_ready_time().
获取source的“就绪时间”,由g_source_set_ready_time()设置。

Any time before the current monotonic time (including 0) is an indication that the source will fire immediately.
当前单调时间之前的任何时间(包括 0)都表示源将立即触发。

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

the monotonic ready time, -1 for "never"
单调就绪时间,-1 表示“从不”


g_source_add_unix_fd () g_source_add_unix_fd()

gpointer
g_source_add_unix_fd (GSource *source,
                      gint fd,
                      GIOCondition events);

Monitors fd for the IO events in events .
监视events中的 IO 事件的fd

The tag returned by this function can be used to remove or modify the monitoring of the fd using g_source_remove_unix_fd() or g_source_modify_unix_fd().
该函数返回的标签可用于使用g_source_remove_unix_fd()g_source_modify_unix_fd()删除或修改 fd 的监控。

It is not necessary to remove the fd before destroying the source; it will be cleaned up automatically.
销毁源之前不需要移除fd;它将被自动清理。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。

Parameters 参数

source 来源

a GSource
一个G源

 

fd

the fd to monitor 要监视的 fd

 

events 事件

an event mask 事件掩码

 

Returns 退货

an opaque tag.  不透明标签。

[not nullable]
[不可为空]

Since: 2.36 自: 2.36


g_source_remove_unix_fd ()
g_source_remove_unix_fd()

void
g_source_remove_unix_fd (GSource *source,
                         gpointer tag);

Reverses the effect of a previous call to g_source_add_unix_fd().
反转先前调用g_source_add_unix_fd()的效果。

You only need to call this if you want to remove an fd from being watched while keeping the same source around. In the normal case you will just want to destroy the source.
仅当您想删除某个 fd 并保持相同的源时,才需要调用此函数。在正常情况下,您只想销毁源。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。

Parameters 参数

source 来源

a GSource
一个G源

 

tag 标签

the tag from g_source_add_unix_fd().
来自g_source_add_unix_fd()的标签。

[not nullable]
[不可为空]

Since: 2.36 自: 2.36


g_source_modify_unix_fd ()

void
g_source_modify_unix_fd (GSource *source,
                         gpointer tag,
                         GIOCondition new_events);

Updates the event mask to watch for the fd identified by tag .
更新事件掩码以监视由tag标识的 fd 。

tag is the tag returned from g_source_add_unix_fd().
tag是从g_source_add_unix_fd()返回的标签。

If you want to remove a fd, don't set its event mask to zero. Instead, call g_source_remove_unix_fd().
如果要删除 fd,请勿将其事件掩码设置为零。相反,请调用g_source_remove_unix_fd()

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。

Parameters 参数

source 来源

a GSource
一个G源

 

tag 标签

the tag from g_source_add_unix_fd().
来自g_source_add_unix_fd()的标签。

[not nullable]
[不可为空]

new_events 新事件

the new event mask to watch
值得观看的新事件掩码

 

Since: 2.36 自: 2.36


g_source_query_unix_fd ()
g_source_query_unix_fd()

GIOCondition
g_source_query_unix_fd (GSource *source,
                        gpointer tag);

Queries the events reported for the fd corresponding to tag on source during the last poll.
查询上次轮询期间sourcetag对应的 fd 报告的事件。

The return value of this function is only defined when the function is called from the check or dispatch functions for source .
仅当从source检查或调度函数调用该函数时,才定义该函数的返回值。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。

Parameters 参数

source 来源

a GSource
一个G源

 

tag 标签

the tag from g_source_add_unix_fd().
来自g_source_add_unix_fd()的标签。

[not nullable]
[不可为空]

Returns 退货

the conditions reported on the fd
FD上报告的情况

Since: 2.36 自: 2.36


g_source_add_poll () g_source_add_poll()

void
g_source_add_poll (GSource *source,
                   GPollFD *fd);

Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined with g_source_new() to add an event source. The event source's check function will typically test the revents field in the GPollFD struct and return TRUE if events need to be processed.
将文件描述符添加到为此源轮询的文件描述符集中。这通常与g_source_new()结合使用来添加事件源。事件源的检查函数通常会测试GPollFD结构中的revents字段,如果需要处理事件则返回TRUE

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

Using this API forces the linear scanning of event sources on each main loop iteration. Newly-written event sources should try to use g_source_add_unix_fd() instead of this API.
使用此 API 会强制在每次主循环迭代上对事件源进行线性扫描。新编写的事件源应尝试使用g_source_add_unix_fd()而不是此 API。

Parameters 参数

source 来源

a GSource
一个G源

 

fd

a GPollFD structure holding information about a file descriptor to watch.
一个GPollFD结构,保存有关要监视的文件描述符的信息。

 

g_source_remove_poll () g_source_remove_poll()

void
g_source_remove_poll (GSource *source,
                      GPollFD *fd);

Removes a file descriptor from the set of file descriptors polled for this source.
从为此源轮询的文件描述符集中删除文件描述符。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

Parameters 参数

source 来源

a GSource
一个G源

 

fd

a GPollFD structure previously passed to g_source_add_poll().
先前传递给g_source_add_poll() GPollFD结构。

 

g_source_add_child_source ()

void
g_source_add_child_source (GSource *source,
                           GSource *child_source);

Adds child_source to source as a "polled" source; when source is added to a GMainContext, child_source will be automatically added with the same priority, when child_source is triggered, it will cause source to dispatch (in addition to calling its own callback), and when source is destroyed, it will destroy child_source as well. (source will also still be dispatched if its own prepare/check functions indicate that it is ready.)
child_source添加到source作为“轮询”源;当source添加到GMainContext时, child_source会自动以相同的优先级添加,当child_source被触发时,它会导致source调度(除了调用它自己的回调),当source被销毁时,它会销毁child_source出色地。 (如果源自己的准备/检查功能表明它已准备好,则仍将分派source 。)

If you don't need child_source to do anything on its own when it triggers, you can call g_source_set_dummy_callback() on it to set a callback that does nothing (except return TRUE if appropriate).
如果您不需要child_source在触发时自行执行任何操作,则可以对其调用g_source_set_dummy_callback()以设置不执行任何操作的回调(如果适用,则返回TRUE除外)。

source will hold a reference on child_source while child_source is attached to it.
child_source附加到它时, source将保留对child_source引用。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

Parameters 参数

source 来源

a GSource
一个G源

 

child_source 子源

a second GSource that source should "poll"
source应该“轮询”的第二个GSource

 

Since: 2.28 自: 2.28


g_source_remove_child_source ()

void
g_source_remove_child_source (GSource *source,
                              GSource *child_source);

Detaches child_source from source and destroys it.
source中分离child_source并销毁它。

This API is only intended to be used by implementations of GSource. Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。

Parameters 参数

source 来源

a GSource
一个G源

 

child_source 子源

a GSource previously passed to g_source_add_child_source().
先前传递给g_source_add_child_source() GSource

 

Since: 2.28 自: 2.28


g_source_get_time () g_source_get_time()

gint64
g_source_get_time (GSource *source);

Gets the time to be used when checking this source. The advantage of calling this function over calling g_get_monotonic_time() directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system monotonic time.
获取检查此源时要使用的时间。与直接调用g_get_monotonic_time()相比,调用此函数的优点是,在检查多个源时,GLib 可以缓存单个值,而不必重复获取系统单调时间。

The time here is the system monotonic time, if available, or some other reasonable alternative otherwise. See g_get_monotonic_time().
这里的时间是系统单调时间(如果可用),或者其他合理的替代时间。请参阅g_get_monotonic_time()

Parameters 参数

source 来源

a GSource
一个G源

 

Returns 退货

the monotonic time in microseconds
以微秒为单位的单调时间

Since: 2.28 自: 2.28


g_source_get_current_time ()
g_source_get_current_time()

void
g_source_get_current_time (GSource *source,
                           GTimeVal *timeval);

g_source_get_current_time has been deprecated since version 2.28 and should not be used in newly-written code.
g_source_get_current_time自版本 2.28 起已被弃用,不应在新编写的代码中使用。

use g_source_get_time() instead
使用g_source_get_time()代替

This function ignores source and is otherwise the same as g_get_current_time().
该函数忽略source ,其他方面与g_get_current_time()相同。

Parameters 参数

source 来源

a GSource
一个G源

 

timeval 时间值

GTimeVal structure in which to store current time.
GTimeVal结构,用于存储当前时间。

 

g_source_remove () g_source_remove()

gboolean
g_source_remove (guint tag);

Removes the source with the given ID from the default main context. You must use g_source_destroy() for sources added to a non-default main context.
从默认主上下文中删除具有给定 ID 的源。对于添加到非默认主上下文的源,必须使用g_source_destroy()

The ID of a GSource is given by g_source_get_id(), or will be returned by the functions g_source_attach(), g_idle_add(), g_idle_add_full(), g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
GSource的 ID 由g_source_get_id()给出,或者由函数g_source_attach()g_idle_add()g_idle_add_full()g_timeout_add()g_timeout_add_full()g_child_watch_add()g_child_watch_add_full()g_io_add_watch()返回和g_io_add_watch_full()

It is a programmer error to attempt to remove a non-existent source.
尝试删除不存在的源是程序员的错误。

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with g_idle_add(): the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.
更具体地说:源 ID 可以在源被销毁后重新发布,因此使用此函数与可能已被删除的源 ID 永远是无效的。一个示例是,当使用g_idle_add()调度空闲在另一个线程中运行时:在其(现在无效)源 ID 上调用此函数时,空闲可能已经运行并被删除。该源 ID 可能已重新发布,从而导致针对错误的源执行操作。

Parameters 参数

tag 标签

the ID of the source to remove.
要删除的源的 ID。

 

Returns 退货

TRUE if the source was found and removed.
如果找到并删除源, TRUE


g_source_remove_by_funcs_user_data ()

gboolean
g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
                                    gpointer user_data);

Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.
给定源函数和用户数据,从默认主循环上下文中删除源。如果存在多个具有相同源功能和用户数据的源,则仅销毁一个。

Parameters 参数

funcs 函数

The source_funcs passed to g_source_new()
传递给g_source_new() source_funcs

 

user_data 用户数据

the user data for the callback
回调的用户数据

 

Returns 退货

TRUE if a source was found and removed.
如果找到并删除源, TRUE


g_source_remove_by_user_data ()

gboolean
g_source_remove_by_user_data (gpointer user_data);

Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.
给定回调的用户数据,从默认主循环上下文中删除源。如果存在多个具有相同用户数据的源,则只有一个将被销毁。

Parameters 参数

user_data 用户数据

the user_data for the callback.
回调的 user_data。

 

Returns 退货

TRUE if a source was found and removed.
如果找到并删除源, TRUE


GClearHandleFunc () GClearHandleFunc()

void
(*GClearHandleFunc) (guint handle_id);

Specifies the type of function passed to g_clear_handle_id(). The implementation is expected to free the resource identified by handle_id ; for instance, if handle_id is a GSource ID, g_source_remove() can be used.
指定传递给g_clear_handle_id()的函数类型。该实现预计会释放由handle_id标识的资源;例如,如果handle_idGSource ID,则可以使用g_source_remove()

Parameters 参数

handle_id 句柄ID

the handle ID to clear
要清除的句柄 ID

 

Since: 2.56 自: 2.56


g_clear_handle_id () g_clear_handle_id()

void
g_clear_handle_id (guint *tag_ptr,
                   GClearHandleFunc clear_func);

Clears a numeric handler, such as a GSource ID.
清除数字处理程序,例如GSource ID。

tag_ptr must be a valid pointer to the variable holding the handler.
tag_ptr必须是指向保存处理程序的变量的有效指针。

If the ID is zero then this function does nothing. Otherwise, clear_func() is called with the ID as a parameter, and the tag is set to zero.
如果 ID 为零,则此函数不执行任何操作。否则,将使用 ID 作为参数调用clear_func() ,并将标记设置为零。

A macro is also included that allows this function to be used without pointer casts.
还包含一个宏,允许在不进行指针转换的情况下使用此函数。

[skip]
[跳过]

Parameters 参数

tag_ptr 标签指针

a pointer to the handler ID.
指向处理程序 ID 的指针。

[not nullable]
[不可为空]

clear_func 清除函数

the function to call to clear the handler.
调用以清除处理程序的函数。

[not nullable]
[不可为空]

Since: 2.56 自: 2.56


g_steal_fd () g_steal_fd()

int
g_steal_fd (int *fd_ptr);

Sets fd_ptr to -1, returning the value that was there before.
fd_ptr设置为-1 ,返回之前的值。

Conceptually, this transfers the ownership of the file descriptor from the referenced variable to the caller of the function (i.e. ‘steals’ the reference). This is very similar to g_steal_pointer(), but for file descriptors.
从概念上讲,这将文件描述符的所有权从引用的变量转移到函数的调用者(即“窃取”引用)。这与g_steal_pointer()非常相似,但针对的是文件描述符。

On POSIX platforms, this function is async-signal safe (see signal(7)) and signal-safety(7))), making it safe to call from a signal handler or a GSpawnChildSetupFunc.
在 POSIX 平台上,此函数是异步信号安全的(请参阅signal(7) )和signal-safety(7) )),从而可以安全地从信号处理程序或GSpawnChildSetupFunc进行调用。

Parameters 参数

fd_ptr

A pointer to a file descriptor.
指向文件描述符的指针。

[not optional][inout]
[不可选][输入输出]

Returns 退货

the value that fd_ptr previously had
fd_ptr之前的值

Since: 2.70 自: 2.70

Types and Values 类型和值

GMainLoop G主循环

typedef struct _GMainLoop GMainLoop;

The GMainLoop struct is an opaque data type representing the main event loop of a GLib or GTK+ application.
GMainLoop结构是一种不透明的数据类型,表示 GLib 或 GTK+ 应用程序的主事件循环。


G_PRIORITY_HIGH

#define G_PRIORITY_HIGH            -100

Use this for high priority event sources.
将此用于高优先级事件源。

It is not used within GLib or GTK+.
它不在 GLib 或 GTK+ 中使用。


G_PRIORITY_DEFAULT

#define G_PRIORITY_DEFAULT          0

Use this for default priority event sources.
将此用于默认优先级事件源。

In GLib this priority is used when adding timeout functions with g_timeout_add(). In GDK this priority is used for events from the X server.
在 GLib 中,当使用g_timeout_add()添加超时函数时使用此优先级。在 GDK 中,此优先级用于来自 X 服务器的事件。


G_PRIORITY_HIGH_IDLE

#define G_PRIORITY_HIGH_IDLE        100

Use this for high priority idle functions.
将此用于高优先级空闲功能。

GTK+ uses G_PRIORITY_HIGH_IDLE + 10 for resizing operations, and G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is done to ensure that any pending resizes are processed before any pending redraws, so that widgets are not redrawn twice unnecessarily.)
GTK+ 使用G_PRIORITY_HIGH_IDLE + 10 进行大小调整操作,使用G_PRIORITY_HIGH_IDLE + 20 进行重绘操作。 (这样做是为了确保在任何挂起的重绘之前处理任何挂起的大小调整,以便部件不会不必要地重绘两次。)


G_PRIORITY_DEFAULT_IDLE

#define G_PRIORITY_DEFAULT_IDLE     200

Use this for default priority idle functions.
将此用于默认优先级空闲功能。

In GLib this priority is used when adding idle functions with g_idle_add().
在 GLib 中,当使用g_idle_add()添加空闲函数时使用此优先级。


G_PRIORITY_LOW

#define G_PRIORITY_LOW              300

Use this for very low priority background tasks.
将此用于非常低优先级的后台任务。

It is not used within GLib or GTK+.
它不在 GLib 或 GTK+ 中使用。


G_SOURCE_CONTINUE

#define G_SOURCE_CONTINUE       TRUE

Use this macro as the return value of a GSourceFunc to leave the GSource in the main loop.
使用此宏作为GSourceFunc的返回值,将GSource保留在主循环中。

Since: 2.32 自: 2.32


G_SOURCE_REMOVE

#define G_SOURCE_REMOVE         FALSE

Use this macro as the return value of a GSourceFunc to remove the GSource from the main loop.
使用此宏作为GSourceFunc的返回值以从主循环中删除GSource

Since: 2.32 自: 2.32


GMainContext G主上下文

typedef struct _GMainContext GMainContext;

The GMainContext struct is an opaque data type representing a set of sources to be handled in a main loop.
GMainContext结构是一种不透明的数据类型,表示要在主循环中处理的一组源。


enum GMainContextFlags 枚举 GMainContextFlags

Flags to pass to g_main_context_new_with_flags() which affect the behaviour of a GMainContext.
要传递到的标志 g_main_context_new_with_flags() 这会影响GMainContext的行为。

Members 会员

G_MAIN_CONTEXT_FLAGS_NONE

Default behaviour. 默认行为。

 

G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING

Assume that polling for events will free the thread to process other jobs. That's useful if you're using g_main_context_{prepare,query,check,dispatch} to integrate GMainContext in other event loops.
假设轮询事件将释放线程来处理其他作业。如果您正在使用,这很有用 g_main_context_{prepare,query,check,dispatch} 将 GMainContext 集成到其他事件循环中。

 

Since: 2.72 自: 2.72


GMainContextPusher

typedef void GMainContextPusher GLIB_AVAILABLE_TYPE_IN_2_64;

Opaque type. See g_main_context_pusher_new() for details.

Since: 2.64


GPid

typedef int GPid;

A type which is used to hold a process identification.
用于保存进程标识的类型。

On UNIX, processes are identified by a process id (an integer), while Windows uses process handles (which are pointers).
在 UNIX 上,进程由进程 ID(整数)标识,而 Windows 使用进程句柄(指针)。

GPid is used in GLib only for descendant processes spawned with the g_spawn functions.
GPid 在 GLib 中仅用于使用 g_spawn 函数生成的后代进程。


G_PID_FORMAT G_PID_格式

#define G_PID_FORMAT "i"

A format specifier that can be used in printf()-style format strings when printing a GPid.
打印GPid时可在printf()样式格式字符串中使用的格式说明符。

Since: 2.50 自: 2.50


struct GPollFD 结构体GPollFD

struct GPollFD {
#if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
#endif
#else
  gint		fd;
#endif
  gushort 	events;
  gushort 	revents;
};

Represents a file descriptor, which events to poll for, and which events occurred.
表示文件描述符、要轮询哪些事件以及发生了哪些事件。

Members 会员

gint fd;
金特fd ;

the file descriptor to poll (or a HANDLE on Win32)
要轮询的文件描述符(或 Win32 上的 HANDLE)

 

gushort events;
古肖特events

a bitwise combination from GIOCondition, specifying which events should be polled for. Typically for reading from a file descriptor you would use G_IO_IN | G_IO_HUP | G_IO_ERR, and for writing you would use G_IO_OUT | G_IO_ERR.
来自GIOCondition的按位组合,指定应轮询哪些事件。通常,要从文件描述符读取,您将使用G_IO_IN | G_IO_HUP | G_IO_ERR ,并且为了编写,您将使用G_IO_OUT | G_IO_OUT | G_IO_ERR 。 G_IO_ERR

 

gushort revents;
古肖特revents ;

a bitwise combination of flags from GIOCondition, returned from the poll() function to indicate which events occurred.
来自GIOCondition的标志的按位组合,从poll()函数返回以指示发生了哪些事件。

 

G_POLLFD_FORMAT

#define G_POLLFD_FORMAT "%d"

A format specifier that can be used in printf()-style format strings when printing the fd member of a GPollFD.
打印GPollFDfd成员时可在printf()样式格式字符串中使用的格式说明符。


struct GSource 结构体GSource

struct GSource {
};

The GSource struct is an opaque data type representing an event source.
GSource结构是表示事件源的不透明数据类型。


struct GSourceFuncs 结构体GSourceFuncs

struct GSourceFuncs {
  gboolean (*prepare)  (GSource    *source,
                        gint       *timeout_);/* Can be NULL */
  gboolean (*check)    (GSource    *source);/* Can be NULL */
  gboolean (*dispatch) (GSource    *source,
                        GSourceFunc callback,
                        gpointer    user_data);
  void     (*finalize) (GSource    *source); /* Can be NULL */
};

The GSourceFuncs struct contains a table of functions used to handle event sources in a generic manner.
GSourceFuncs结构包含一个用于以通用方式处理事件源的函数表。

For idle sources, the prepare and check functions always return TRUE to indicate that the source is always ready to be processed. The prepare function also returns a timeout value of 0 to ensure that the poll() call doesn't block (since that would be time wasted which could have been spent running the idle function).
对于空闲源,准备和检查函数始终返回TRUE以指示源始终准备好进行处理。准备函数还返回超时值 0,以确保poll()调用不会阻塞(因为这会浪费时间,而这些时间本来可以花在运行空闲函数上)。

For timeout sources, the prepare and check functions both return TRUE if the timeout interval has expired. The prepare function also returns a timeout value to ensure that the poll() call doesn't block too long and miss the next timeout.
对于超时源,如果超时间隔已过期,则准备和检查函数均返回TRUE 。准备函数还返回一个超时值,以确保poll()调用不会阻塞太久而错过下一次超时。

For file descriptor sources, the prepare function typically returns FALSE, since it must wait until poll() has been called before it knows whether any events need to be processed. It sets the returned timeout to -1 to indicate that it doesn't mind how long the poll() call blocks. In the check function, it tests the results of the poll() call to see if the required condition has been met, and returns TRUE if so.
对于文件描述符源,prepare 函数通常返回FALSE ,因为它必须等到调用poll()后才能知道是否需要处理任何事件。它将返回的超时设置为 -1,表示它不介意poll()调用阻塞多长时间。在 check 函数中,它测试poll()调用的结果以查看是否满足所需条件,如果满足则返回TRUE

Members 会员

prepare ()
prepare ()

Called before all the file descriptors are polled. If the source can determine that it is ready here (without waiting for the results of the poll() call) it should return TRUE. It can also return a timeout_ value which should be the maximum timeout (in milliseconds) which should be passed to the poll() call. The actual timeout used will be -1 if all sources returned -1, or it will be the minimum of all the timeout_ values returned which were >= 0. Since 2.36 this may be NULL, in which case the effect is as if the function always returns FALSE with a timeout of -1. If prepare returns a timeout and the source also has a ready time set, then the lower of the two will be used.
在轮询所有文件描述符之前调用。如果源可以确定它已准备就绪(无需等待poll()调用的结果),则它应该返回TRUE 。它还可以返回一个timeout_值,该值应该是应该传递给poll()调用的最大超时(以毫秒为单位)。如果所有源都返回 -1,则实际使用的超时将为 -1,或者它将是返回的所有timeout_值(>= 0)中的最小值。从 2.36 开始,这可能是NULL ,在这种情况下,效果就像函数始终返回FALSE ,超时为 -1。如果prepare返回超时并且源也设置了就绪时间,则将使用两者中较小的一个。

 

check ()
check ()

Called after all the file descriptors are polled. The source should return TRUE if it is ready to be dispatched. Note that some time may have passed since the previous prepare function was called, so the source should be checked again here. Since 2.36 this may be NULL, in which case the effect is as if the function always returns FALSE.
在轮询所有文件描述符后调用。如果源已准备好分派,则应返回TRUE 。请注意,自调用上一个准备函数以来可能已经过去了一段时间,因此应在此处再次检查源。从 2.36 开始,这可能是NULL ,在这种情况下,效果就好像函数总是返回FALSE

 

dispatch ()
dispatch ()

Called to dispatch the event source, after it has returned TRUE in either its prepare or its check function, or if a ready time has been reached. The dispatch function receives a callback function and user data. The callback function may be NULL if the source was never connected to a callback using g_source_set_callback(). The dispatch function should call the callback function with user_data and whatever additional parameters are needed for this type of event source. The return value of the dispatch function should be G_SOURCE_REMOVE if the source should be removed or G_SOURCE_CONTINUE to keep it.
在事件源在其preparecheck函数中返回TRUE之后,或者如果已达到就绪时间,则调用以分派事件源。 dispatch函数接收回调函数和用户数据。如果源从未使用g_source_set_callback()连接到回调,则回调函数可能为NULLdispatch函数应使用user_data以及此类事件源所需的任何其他参数来调用回调函数。如果应删除源,则dispatch函数的返回值应为G_SOURCE_REMOVE ;若要保留源,则调度函数的返回值应为G_SOURCE_CONTINUE

 

finalize ()
finalize ()

Called when the source is finalized. At this point, the source will have been destroyed, had its callback cleared, and have been removed from its GMainContext, but it will still have its final reference count, so methods can be called on it from within this function.
当源完成时调用。此时,源将被销毁,其回调被清除,并已从其GMainContext中删除,但它仍将具有其最终引用计数,因此可以从此函数内对其调用方法。

 

struct GSourceCallbackFuncs
结构体GSourceCallbackFuncs

struct GSourceCallbackFuncs {
  void (*ref)   (gpointer     cb_data);
  void (*unref) (gpointer     cb_data);
  void (*get)   (gpointer     cb_data,
                 GSource     *source,

                 GSourceFunc *func,
                 gpointer    *data);
};

The GSourceCallbackFuncs struct contains functions for managing callback objects.
GSourceCallbackFuncs结构包含用于管理回调对象的函数。

Members 会员

ref ()
ref ()

Called when a reference is added to the callback object
当引用添加到回调对象时调用

 

unref ()
unref ()

Called when a reference to the callback object is dropped
当对回调对象的引用被删除时调用

 

get ()
get ()

Called to extract the callback function and data from the callback object.
调用以从回调对象中提取回调函数和数据。