glib-The Main Event Loop

参考:
https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#GMainContext
Description:

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

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

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

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

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.

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.

GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending().

主事件循环管理所有可用的事件源(为GLib和GTK+应用程序)。 这些事件可以来自任何数量不同的源,例如文件描述符(一般文件,管道,套接字)与超时。

新的事件类型源也可以使用g_source_attach()添加。
允许多个独立的源集合在不同的线程中处理,每一个源关联一个GMainContext,一个GMainContext仅仅能运行在单个线程中。但是源可以在其他线程中加入
或是删除它(GMainContext)
每个事件源被分配一个优先级。默认的优先级是:G_PRIORITY_DEFAULT,其值是0.值小于0代表更高的优先级。值大于0代表更低的优先级。高优先级事件先处理,
低优先级事件后处理。
空闲函数也能被增加,分配一个优先级。只要没有更高优先级的事件准备好处理,这些将会运行。
GMainLoop数据类型代表一个主事件循环。一个GMainLoop是被g_main_loop_new()创建。添加初始事件源后,g_main_loop_run()被调用。这个将持续从
每一个事件源检查新事件,并分发他们。最后,来自一个源的被处理事件,通过调用g_main_loop_quit()去推出主循环, 并g_main_loop_run()返回。
可以递归地创建新的GMainLoop实例。这点是经常使用在GTK+应用程序中,当显示对话框模型。请注意,事件源与特定的GMainContext相关联, 并将检查与分
派针对关联GMainContext的所有主循环。
GTK+包含部分这些函数的封装。如gtk_main(), gtk_main_quit(),gtk_events_pending()等。

Creating new sources types:
One of the unusual features of the GTK+ main loop 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 types.

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().

GTK+主循环功能的不寻常的特征是,除了内建事件源类型,还可以创建和使用新的事件源。一个新的事件源类型是被使用处理GDK 事件。

通过从GSource结构派生(deriving)而创建新的源类型。这个派生源的类型被一个结构代表,这个结构的第一个元素为Gsource结构。其他元素针对(specific to)新的源类型。
用一个新的源类型创建实例,调用g_source_new(),通过这个派生结构的大小与函数表。 这些GSourceFuncs决定这个新源类型的行为。
新的源类型主要通过两种方式与主循环进行交互。他们的准备函数(在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().

The operation of these functions can best be seen in terms of a state diagram, as shown in Figure 1, “States of a Main Context”.
能使用g_main_context_iteration(),运行一个GMainContext的单迭代。 在某些情况下,需要更详细地控制主循环的细节是如何工作的,例如,当将GMainLoop与外部主回路集成时
在这样的情况,你能直接调用g_mian_context_iteration()组件功能。这些函数是 g_main_context_prepare(),g_main_context_query(), g_main_context_check(),
g_main_context_dispatch().
这些功能的操作可以从状态图中看出来,展示在表一“States of a Main Context”

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值