📄 glib-the-main-event-loop.html
字号:
<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify); <a class="link" href="glib-The-Main-Event-Loop.html#GPollFD">GPollFD</a>; <a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a>;void (<a class="link" href="glib-The-Main-Event-Loop.html#GSourceDummyMarshal">*GSourceDummyMarshal</a>) (void); <a class="link" href="glib-The-Main-Event-Loop.html#GSourceFuncs">GSourceFuncs</a>; <a class="link" href="glib-The-Main-Event-Loop.html#GSourceCallbackFuncs">GSourceCallbackFuncs</a>;<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a>* <a class="link" href="glib-The-Main-Event-Loop.html#g-source-new">g_source_new</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSourceFuncs">GSourceFuncs</a> *source_funcs, <a class="link" href="glib-Basic-Types.html#guint">guint</a> struct_size);<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a>* <a class="link" href="glib-The-Main-Event-Loop.html#g-source-ref">g_source_ref</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-unref">g_source_unref</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-set-funcs">g_source_set_funcs</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-The-Main-Event-Loop.html#GSourceFuncs">GSourceFuncs</a> *funcs);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-attach">g_source_attach</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext">GMainContext</a> *context);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-destroy">g_source_destroy</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-is-destroyed">g_source_is_destroyed</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-set-priority">g_source_set_priority</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-Basic-Types.html#gint">gint</a> priority);<a class="link" href="glib-Basic-Types.html#gint">gint</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-get-priority">g_source_get_priority</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-set-can-recurse">g_source_set_can_recurse</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> can_recurse);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-get-can-recurse">g_source_get_can_recurse</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);<a class="link" href="glib-Basic-Types.html#guint">guint</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-get-id">g_source_get_id</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);<a class="link" href="glib-The-Main-Event-Loop.html#GMainContext">GMainContext</a>* <a class="link" href="glib-The-Main-Event-Loop.html#g-source-get-context">g_source_get_context</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-set-callback">g_source_set_callback</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-The-Main-Event-Loop.html#GSourceFunc">GSourceFunc</a> func, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data, <a class="link" href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSourceFunc">*GSourceFunc</a>) (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> data);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-set-callback-indirect">g_source_set_callback_indirect</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> callback_data, <a class="link" href="glib-The-Main-Event-Loop.html#GSourceCallbackFuncs">GSourceCallbackFuncs</a> *callback_funcs);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-add-poll">g_source_add_poll</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-The-Main-Event-Loop.html#GPollFD">GPollFD</a> *fd);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-remove-poll">g_source_remove_poll</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-The-Main-Event-Loop.html#GPollFD">GPollFD</a> *fd);void <a class="link" href="glib-The-Main-Event-Loop.html#g-source-get-current-time">g_source_get_current_time</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSource">GSource</a> *source, <a class="link" href="glib-Date-and-Time-Functions.html#GTimeVal">GTimeVal</a> *timeval);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-remove">g_source_remove</a> (<a class="link" href="glib-Basic-Types.html#guint">guint</a> tag);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-remove-by-funcs-user-data">g_source_remove_by_funcs_user_data</a> (<a class="link" href="glib-The-Main-Event-Loop.html#GSourceFuncs">GSourceFuncs</a> *funcs, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> <a class="link" href="glib-The-Main-Event-Loop.html#g-source-remove-by-user-data">g_source_remove_by_user_data</a> (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);</pre></div><div class="refsect1" lang="en"><a name="id2856540"></a><h2>Description</h2><p> 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 <a class="link" href="glib-The-Main-Event-Loop.html#g-source-attach"><code class="function">g_source_attach()</code></a>. </p><p> To allow multiple independent sets of sources to be handled in different threads, each source is associated with a <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a>. A <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a> can only be running in a single thread, but sources can be added to it and removed from it from other threads. </p><p> Each event source is assigned a priority. The default priority, <a class="link" href="glib-The-Main-Event-Loop.html#G-PRIORITY-DEFAULT:CAPS"><span class="type">G_PRIORITY_DEFAULT</span></a>, 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. </p><p> 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. </p><p> The <a class="link" href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> data type represents a main event loop. A <a class="link" href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> is created with <a class="link" href="glib-The-Main-Event-Loop.html#g-main-loop-new"><code class="function">g_main_loop_new()</code></a>. After adding the initial event sources, <a class="link" href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a> 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 <a class="link" href="glib-The-Main-Event-Loop.html#g-main-loop-quit"><code class="function">g_main_loop_quit()</code></a> to exit the main loop, and <a class="link" href="glib-The-Main-Event-Loop.html#g-main-loop-run"><code class="function">g_main_loop_run()</code></a> returns. </p><p> It is possible to create new instances of <a class="link" href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a>, and will be checked and dispatched for all main loops associated with that <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a>. </p><p> GTK+ contains wrappers of some of these functions, e.g. <ahref="/usr/share/gtk-doc/html/gtk/gtk-General.html#gtk-main"><code class="function">gtk_main()</code></a>, <ahref="/usr/share/gtk-doc/html/gtk/gtk-General.html#gtk-main-quit"><code class="function">gtk_main_quit()</code></a> and <ahref="/usr/share/gtk-doc/html/gtk/gtk-General.html#gtk-events-pending"><code class="function">gtk_events_pending()</code></a>. </p><div class="refsect2" lang="en"><a name="id2856757"></a><h3>Creating new sources types</h3><p> 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 <em class="firstterm">deriving</em> from the <a class="link" href="glib-The-Main-Event-Loop.html#GSource"><span class="type">GSource</span></a> structure. The derived type of source is represented by a structure that has the <a class="link" href="glib-The-Main-Event-Loop.html#GSource"><span class="type">GSource</span></a> structure as a first element, and other elements specific to the new source type. To create an instance of the new source type, call <a class="link" href="glib-The-Main-Event-Loop.html#g-source-new"><code class="function">g_source_new()</code></a> passing in the size of the derived structure and a table of functions. These <a class="link" href="glib-The-Main-Event-Loop.html#GSourceFuncs"><span class="type">GSourceFuncs</span></a> determine the behavior of the new source types. </p><p> New source types basically interact with the main context in two ways. Their prepare function in <a class="link" href="glib-The-Main-Event-Loop.html#GSourceFuncs"><span class="type">GSourceFuncs</span></a> 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 <a class="link" href="glib-The-Main-Event-Loop.html#g-source-add-poll"><code class="function">g_source_add_poll()</code></a>. </p></div><hr><div class="refsect2" lang="en"><a name="id2856845"></a><h3>Customizing the main loop iteration</h3><p> Single iterations of a <a class="link" href="glib-The-Main-Event-Loop.html#GMainContext"><span class="type">GMainContext</span></a> can be run with <a class="link" href="glib-The-Main-Event-Loop.html#g-main-context-iteration"><code class="function">g_main_context_iteration()</code></a>. In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the <a class="link" href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> with an external main loop. In such cases, you can call the component functions of <a class="link" href="glib-The-Main-Event-Loop.html#g-main-context-iteration"><code class="function">g_main_context_iteration()</code></a> directly. These functions are <a class="link" href="glib-The-Main-Event-Loop.html#g-main-context-prepare"><code class="function">g_main_context_prepare()</code></a>, <a class="link" href="glib-The-Main-Event-Loop.html#g-main-context-query"><code class="function">g_main_context_query()</code></a>, <a class="link" href="glib-The-Main-Event-Loop.html#g-main-context-check"><code class="function">g_main_context_check()</code></a> and <a class="link" href="glib-The-Main-Event-Loop.html#g-main-context-dispatch"><code class="function">g_main_context_dispatch()</code></a>. </p><p> The operation of these functions can best be seen in terms of a state diagram, as shown in <a class="xref" href="glib-The-Main-Event-Loop.html#mainloop-states" title="Figure 1. States of a Main Context">Figure 1, “States of a Main Context”</a>. </p><div class="figure"><a name="mainloop-states"></a><p class="title"><b>Figure 1. States of a Main Context</b></p><div class="figure-contents"><div><img src="mainloop-states.gif" alt="States of a Main Context"></div></div></div><br class="figure-break"></div></div><div class="refsect1" lang="en"><a name="id2856976"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2856986"></a><h3><a name="GMainLoop"></a>GMainLoop</h3><a class="indexterm" name="id2856998"></a><pre class="programlisting">typedef struct _GMainLoop GMainLoop;</pre><p>The <span class="structname">GMainLoop</span> struct is an opaque data type representing the main event loop of a GLib or GTK+ application.</p></div><hr><div class="refsect2" lang="en"><a name="id2857019"></a><h3><a name="g-main-loop-new"></a>g_main_loop_new ()</h3><a class="indexterm" name="id2857032"></a><pre class="programlisting"><a class="link" href="glib-The-Main-Event-Loop.html#GMainLoop">GMainLoop</a>* g_main_loop_new (<a class="link" href="glib-The-Main-Event-Loop.html#GMainContext">GMainContext</a> *context, <a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> is_running);</pre><p>Creates a new <a class="link" href="glib-The-Main-Event-Loop.html#GMainLoop"><span class="type">GMainLoop</span></a> structure.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -