⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdk-threads.html

📁 最新gtk中文资料集
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Threads</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><link rel="start" href="index.html" title="GDK Reference Manual"><link rel="up" href="reference.html" title="API Reference"><link rel="prev" href="gdk-Properties-and-Atoms.html" title="Properties and Atoms"><link rel="next" href="gdk-Input.html" title="Input"><meta name="generator" content="GTK-Doc V1.9 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="reference" href="reference.html" title="API Reference"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="gdk-Properties-and-Atoms.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="reference.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GDK Reference Manual</th><td><a accesskey="n" href="gdk-Input.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr><tr><td colspan="5" class="shortcuts"><nobr><a href="#id3280628" class="shortcut">Top</a>                  &#160;|&#160;                  <a href="#id3280922" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="gdk-Threads"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id3280628"></a><span class="refentrytitle">Threads</span></h2><p>Threads &#8212; Functions for using GDK in multi-threaded programs</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include &lt;gdk/gdk.h&gt;#define             <a class="link" href="gdk-Threads.html#GDK-THREADS-ENTER:CAPS">GDK_THREADS_ENTER</a>                   ()#define             <a class="link" href="gdk-Threads.html#GDK-THREADS-LEAVE:CAPS">GDK_THREADS_LEAVE</a>                   ()void                <a class="link" href="gdk-Threads.html#gdk-threads-init">gdk_threads_init</a>                    (void);void                <a class="link" href="gdk-Threads.html#gdk-threads-enter">gdk_threads_enter</a>                   (void);void                <a class="link" href="gdk-Threads.html#gdk-threads-leave">gdk_threads_leave</a>                   (void);extern              GMutex *<a class="link" href="gdk-Threads.html#gdk-threads-mutex">gdk_threads_mutex</a>;void                <a class="link" href="gdk-Threads.html#gdk-threads-set-lock-functions">gdk_threads_set_lock_functions</a>      (<ahref="/usr/share/gtk-doc/html/gobject/gobject-Closures.html#GCallback">GCallback</a> enter_fn,                                                         <ahref="/usr/share/gtk-doc/html/gobject/gobject-Closures.html#GCallback">GCallback</a> leave_fn);<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#guint">guint</a>               <a class="link" href="gdk-Threads.html#gdk-threads-add-idle">gdk_threads_add_idle</a>                (<ahref="/usr/share/gtk-doc/html/glib/glib-The-Main-Event-Loop.html#GSourceFunc">GSourceFunc</a> function,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#gpointer">gpointer</a> data);<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#guint">guint</a>               <a class="link" href="gdk-Threads.html#gdk-threads-add-idle-full">gdk_threads_add_idle_full</a>           (<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#gint">gint</a> priority,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-The-Main-Event-Loop.html#GSourceFunc">GSourceFunc</a> function,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#gpointer">gpointer</a> data,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify);<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#guint">guint</a>               <a class="link" href="gdk-Threads.html#gdk-threads-add-timeout">gdk_threads_add_timeout</a>             (<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#guint">guint</a> interval,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-The-Main-Event-Loop.html#GSourceFunc">GSourceFunc</a> function,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#gpointer">gpointer</a> data);<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#guint">guint</a>               <a class="link" href="gdk-Threads.html#gdk-threads-add-timeout-full">gdk_threads_add_timeout_full</a>        (<ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#gint">gint</a> priority,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#guint">guint</a> interval,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-The-Main-Event-Loop.html#GSourceFunc">GSourceFunc</a> function,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Basic-Types.html#gpointer">gpointer</a> data,                                                         <ahref="/usr/share/gtk-doc/html/glib/glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify);</pre></div><div class="refsect1" lang="en"><a name="id3280922"></a><h2>Description</h2><p>For thread safety, GDK relies on the thread primitives in GLib, and on the thread-safe GLib main loop.</p><p>GLib is completely thread safe (all global data is automatically locked), but individual data structure instances are not automatically locked for performance reasons. So e.g. you must coordinate accesses to the same <ahref="/usr/share/gtk-doc/html/glib/glib-Hash-Tables.html#GHashTable"><span class="type">GHashTable</span></a> from multiple threads.</p><p>GTK+ is "thread aware" but not thread safe &#8212; it provides aglobal lock controlled by <a class="link" href="gdk-Threads.html#gdk-threads-enter"><code class="function">gdk_threads_enter()</code></a>/<a class="link" href="gdk-Threads.html#gdk-threads-leave"><code class="function">gdk_threads_leave()</code></a>which protects all use of GTK+. That is, only one thread can use GTK+ at any given time.</p><p>Unfortunately the above holds with the X11 backend only. With theWin32 backend, GDK calls should not be attempted from multiple threadsat all.</p><p>You must call <ahref="/usr/share/gtk-doc/html/glib/glib-Threads.html#g-thread-init"><code class="function">g_thread_init()</code></a> and <a class="link" href="gdk-Threads.html#gdk-threads-init"><code class="function">gdk_threads_init()</code></a> before executingany other GTK+ or GDK functions in a threaded GTK+ program.</p><p>Idles, timeouts, and input functions from GLib, such as <ahref="/usr/share/gtk-doc/html/glib/glib-The-Main-Event-Loop.html#g-idle-add"><code class="function">g_idle_add()</code></a>, areexecuted outside of the main GTK+ lock.So, if you need to call GTK+ inside of such a callback, you must surroundthe callback with a <a class="link" href="gdk-Threads.html#gdk-threads-enter"><code class="function">gdk_threads_enter()</code></a>/<a class="link" href="gdk-Threads.html#gdk-threads-leave"><code class="function">gdk_threads_leave()</code></a> pair or use<a class="link" href="gdk-Threads.html#gdk-threads-add-idle-full"><code class="function">gdk_threads_add_idle_full()</code></a> which does this for you.However, event dispatching from the mainloop is still executed withinthe main GTK+ lock, so callback functions connected to event signalslike GtkWidget::button-press-event, do not need thread protection.</p><p>In particular, this means, if you are writing widgets that might be used in threaded programs, you <span class="emphasis"><em>must</em></span> surround timeouts and idle functions in this matter.</p><p>As always, you must also surround any calls to GTK+ not made within a signal handler with a <a class="link" href="gdk-Threads.html#gdk-threads-enter"><code class="function">gdk_threads_enter()</code></a>/<a class="link" href="gdk-Threads.html#gdk-threads-leave"><code class="function">gdk_threads_leave()</code></a> pair.</p><p>Before calling <a class="link" href="gdk-Threads.html#gdk-threads-leave"><code class="function">gdk_threads_leave()</code></a> from a thread otherthan your main thread, you probably want to call <a class="link" href="gdk-General.html#gdk-flush"><code class="function">gdk_flush()</code></a>to send all pending commands to the windowing system.(The reason you don't need to do this from the main threadis that GDK always automatically flushes pending commandswhen it runs out of incoming events to process and hasto sleep while waiting for more events.)</p><p>A minimal main program for a threaded GTK+ applicationlooks like:</p><div class="informalexample"><pre class="programlisting">intmain (int argc, char *argv[]){  GtkWidget *window;  g_thread_init (NULL);  gdk_threads_init ();  gdk_threads_enter ();  gtk_init (&amp;argc, &amp;argv);  window = create_window ();  gtk_widget_show (window);  gtk_main ();  gdk_threads_leave ();  return 0;}</pre></div><p>Callbacks require a bit of attention. Callbacks from GTK+ signalsare made within the GTK+ lock. However callbacks from GLib (timeouts,IO callbacks, and idle functions) are made outside of the GTK+lock. So, within a signal handler you do not need to call<a class="link" href="gdk-Threads.html#gdk-threads-enter"><code class="function">gdk_threads_enter()</code></a>, but within the other types of callbacks, youdo.</p><p>Erik Mouw contributed the following code example toillustrate how to use threads within GTK+ programs.</p><div class="informalexample"><pre class="programlisting">/*------------------------------------------------------------------------- * Filename:      gtk-thread.c * Version:       0.99.1 * Copyright:     Copyright (C) 1999, Erik Mouw * Author:        Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt; * Description:   GTK threads example.  * Created at:    Sun Oct 17 21:27:09 1999 * Modified by:   Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt; * Modified at:   Sun Oct 24 17:21:41 1999 *-----------------------------------------------------------------------*//* * Compile with: * * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread` * * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some * bugs. * */#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;#include &lt;unistd.h&gt;#include &lt;time.h&gt;#include &lt;gtk/gtk.h&gt;#include &lt;glib.h&gt;#include &lt;pthread.h&gt;#define YES_IT_IS    (1)#define NO_IT_IS_NOT (0)typedef struct {  GtkWidget *label;  int what;} yes_or_no_args;G_LOCK_DEFINE_STATIC (yes_or_no);static volatile int yes_or_no = YES_IT_IS;void destroy (GtkWidget *widget, gpointer data){  gtk_main_quit ();}void *argument_thread (void *args){  yes_or_no_args *data = (yes_or_no_args *)args;  gboolean say_something;  for (;;)    {      /* sleep a while */      sleep(rand() / (RAND_MAX / 3) + 1);      /* lock the yes_or_no_variable */      G_LOCK(yes_or_no);      /* do we have to say something? */      say_something = (yes_or_no != data-&gt;what);      if(say_something)	{	  /* set the variable */	  yes_or_no = data-&gt;what;	}      /* Unlock the yes_or_no variable */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -