<?xml version="1.0"?>
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
               "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
<!ENTITY % local.common.attrib "xmlns:xi  CDATA  #FIXED 'http://www.w3.org/2003/XInclude'">
<!ENTITY version SYSTEM "version.xml">
]>
<refentry id="gdk-Threads">
<refmeta>
<refentrytitle role="top_of_page" id="gdk-Threads.top_of_page">Threads</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GDK Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Threads</refname>
<refpurpose>Functions for using GDK in multi-threaded programs</refpurpose>
</refnamediv>

<refsynopsisdiv id="gdk-Threads.synopsis" role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>

#include &lt;gdk/gdk.h&gt;

#define             <link linkend="GDK-THREADS-ENTER--CAPS">GDK_THREADS_ENTER</link>                   ()
#define             <link linkend="GDK-THREADS-LEAVE--CAPS">GDK_THREADS_LEAVE</link>                   ()
<link linkend="void">void</link>                <link linkend="gdk-threads-init">gdk_threads_init</link>                    (void);
<link linkend="void">void</link>                <link linkend="gdk-threads-enter">gdk_threads_enter</link>                   (void);
<link linkend="void">void</link>                <link linkend="gdk-threads-leave">gdk_threads_leave</link>                   (void);
extern              GMutex *<link linkend="gdk-threads-mutex">gdk_threads_mutex</link>;
<link linkend="void">void</link>                <link linkend="gdk-threads-set-lock-functions">gdk_threads_set_lock_functions</link>      (<link linkend="GCallback">GCallback</link> enter_fn,
                                                         <link linkend="GCallback">GCallback</link> leave_fn);
<link linkend="guint">guint</link>               <link linkend="gdk-threads-add-idle">gdk_threads_add_idle</link>                (<link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data);
<link linkend="guint">guint</link>               <link linkend="gdk-threads-add-idle-full">gdk_threads_add_idle_full</link>           (<link linkend="gint">gint</link> priority,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data,
                                                         <link linkend="GDestroyNotify">GDestroyNotify</link> notify);
<link linkend="guint">guint</link>               <link linkend="gdk-threads-add-timeout">gdk_threads_add_timeout</link>             (<link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data);
<link linkend="guint">guint</link>               <link linkend="gdk-threads-add-timeout-full">gdk_threads_add_timeout_full</link>        (<link linkend="gint">gint</link> priority,
                                                         <link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data,
                                                         <link linkend="GDestroyNotify">GDestroyNotify</link> notify);
<link linkend="guint">guint</link>               <link linkend="gdk-threads-add-timeout-seconds">gdk_threads_add_timeout_seconds</link>     (<link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data);
<link linkend="guint">guint</link>               <link linkend="gdk-threads-add-timeout-seconds-full">gdk_threads_add_timeout_seconds_full</link>
                                                        (<link linkend="gint">gint</link> priority,
                                                         <link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data,
                                                         <link linkend="GDestroyNotify">GDestroyNotify</link> notify);
</synopsis>
</refsynopsisdiv>









<refsect1 id="gdk-Threads.description" role="desc">
<title role="desc.title">Description</title>
<para>
For thread safety, GDK relies on the thread primitives in GLib,
and on the thread-safe GLib main loop.
</para>
<para>
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 <link linkend="GHashTable"><type>GHashTable</type></link> from multiple threads.
</para>
<para>
GTK+ is "thread aware" but not thread safe &mdash; it provides a
global lock controlled by <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>/<link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link>
which protects all use of GTK+. That is, only one thread can use GTK+
at any given time.
</para>
<para>
Unfortunately the above holds with the X11 backend only. With the
Win32 backend, GDK calls should not be attempted from multiple threads
at all.
</para>
<para>
You must call <link linkend="g-thread-init"><function>g_thread_init()</function></link> and <link linkend="gdk-threads-init"><function>gdk_threads_init()</function></link> before executing
any other GTK+ or GDK functions in a threaded GTK+ program.
</para>
<para>
Idles, timeouts, and input functions from GLib, such as <link linkend="g-idle-add"><function>g_idle_add()</function></link>, are
executed outside of the main GTK+ lock.
So, if you need to call GTK+ inside of such a callback, you must surround
the callback with a <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>/<link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link> pair or use
<link linkend="gdk-threads-add-idle-full"><function>gdk_threads_add_idle_full()</function></link> which does this for you.
However, event dispatching from the mainloop is still executed within
the main GTK+ lock, so callback functions connected to event signals
like GtkWidget::button-press-event, do not need thread protection.
</para>
<para>
In particular, this means, if you are writing widgets that might
be used in threaded programs, you <emphasis>must</emphasis> surround
timeouts and idle functions in this matter.
</para>
<para>
As always, you must also surround any calls to GTK+ not made within
a signal handler with a <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>/<link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link> pair.
</para>
<para>
Before calling <link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link> from a thread other
than your main thread, you probably want to call <link linkend="gdk-flush"><function>gdk_flush()</function></link>
to send all pending commands to the windowing system.
(The reason you don't need to do this from the main thread
is that GDK always automatically flushes pending commands
when it runs out of incoming events to process and has
to sleep while waiting for more events.)
</para>
<para>A minimal main program for a threaded GTK+ application
looks like:</para>
<informalexample>
<programlisting role="C">
int
main (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;
}
</programlisting>
</informalexample>
<para>
Callbacks require a bit of attention. Callbacks from GTK+ signals
are 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
<link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>, but within the other types of callbacks, you
do.
</para>
<para>Erik Mouw contributed the following code example to
illustrate how to use threads within GTK+ programs.
</para>
<informalexample>
<programlisting role="C">
/*-------------------------------------------------------------------------
 * 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.
 *
 */
&num;include &lt;stdio.h&gt;
&num;include &lt;stdlib.h&gt;
&num;include &lt;unistd.h&gt;
&num;include &lt;time.h&gt;
&num;include &lt;gtk/gtk.h&gt;
&num;include &lt;glib.h&gt;
&num;include &lt;pthread.h&gt;
&num;define YES_IT_IS    (1)
&num;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->what);
      if(say_something)
	{
	  /* set the variable */
	  yes_or_no = data->what;
	}
      /* Unlock the yes_or_no variable */
      G_UNLOCK (yes_or_no);
      if (say_something)
	{
	  /* get GTK thread lock */
	  gdk_threads_enter (<!-- -->);
	  /* set label text */
	  if(data->what == YES_IT_IS)
	    gtk_label_set_text (GTK_LABEL (data->label), "O yes, it is!");
	  else
	    gtk_label_set_text (GTK_LABEL (data->label), "O no, it isn't!");
	  /* release GTK thread lock */
	  gdk_threads_leave (<!-- -->);
	}
    }
  return NULL;
}
int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *label;
  yes_or_no_args yes_args, no_args;
  pthread_t no_tid, yes_tid;
  /* init threads */
  g_thread_init (NULL);
  gdk_threads_init (<!-- -->);
  gdk_threads_enter (<!-- -->);
  /* init gtk */
  gtk_init(&amp;argc, &amp;argv);
  /* init random number generator */
  srand ((unsigned int) time (NULL));
  /* create a window */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  /* create a label */
  label = gtk_label_new ("And now for something completely different ...");
  gtk_container_add (GTK_CONTAINER (window), label);
  /* show everything */
  gtk_widget_show (label);
  gtk_widget_show (window);
  /* create the threads */
  yes_args.label = label;
  yes_args.what = YES_IT_IS;
  pthread_create (&amp;yes_tid, NULL, argument_thread, &amp;yes_args);
  no_args.label = label;
  no_args.what = NO_IT_IS_NOT;
  pthread_create (&amp;no_tid, NULL, argument_thread, &amp;no_args);
  /* enter the GTK main loop */
  gtk_main (<!-- -->);
  gdk_threads_leave (<!-- -->);
  return 0;
}
</programlisting>
</informalexample>
</refsect1>

<refsect1 id="gdk-Threads.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="GDK-THREADS-ENTER--CAPS" role="macro">
<title>GDK_THREADS_ENTER()</title>
<indexterm zone="GDK-THREADS-ENTER--CAPS"><primary sortas="THREADS_ENTER">GDK_THREADS_ENTER</primary></indexterm><programlisting>#define             GDK_THREADS_ENTER()</programlisting>
<para>
This macro marks the beginning of a critical section in which GDK and
GTK+ functions can be called safely and without causing race
conditions.  Only one thread at a time can be in such a critial
section. The macro expands to a no-op if <link linkend="G-THREADS-ENABLED--CAPS"><type>G_THREADS_ENABLED</type></link> has not
been defined. Typically <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link> should be used instead of
this macro.
</para></refsect2>
<refsect2 id="GDK-THREADS-LEAVE--CAPS" role="macro">
<title>GDK_THREADS_LEAVE()</title>
<indexterm zone="GDK-THREADS-LEAVE--CAPS"><primary sortas="THREADS_LEAVE">GDK_THREADS_LEAVE</primary></indexterm><programlisting>#define             GDK_THREADS_LEAVE()</programlisting>
<para>
This macro marks the end of a critical section
begun with <link linkend="GDK-THREADS-ENTER--CAPS"><type>GDK_THREADS_ENTER</type></link>.
</para></refsect2>
<refsect2 id="gdk-threads-init" role="function">
<title>gdk_threads_init ()</title>
<indexterm zone="gdk-threads-init"><primary sortas="threads_init">gdk_threads_init</primary></indexterm><programlisting><link linkend="void">void</link>                gdk_threads_init                    (void);</programlisting>
<para>
Initializes GDK so that it can be used from multiple threads
in conjunction with <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link> and <link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link>.
<link linkend="g-thread-init"><function>g_thread_init()</function></link> must be called previous to this function.
</para>
<para>
This call must be made before any use of the main loop from
GTK+; to be safe, call it before <link linkend="gtk-init"><function>gtk_init()</function></link>.</para>
<para>
</para></refsect2>
<refsect2 id="gdk-threads-enter" role="function">
<title>gdk_threads_enter ()</title>
<indexterm zone="gdk-threads-enter"><primary sortas="threads_enter">gdk_threads_enter</primary></indexterm><programlisting><link linkend="void">void</link>                gdk_threads_enter                   (void);</programlisting>
<para>
This macro marks the beginning of a critical section in which GDK and
GTK+ functions can be called safely and without causing race
conditions.  Only one thread at a time can be in such a critial
section.
</para></refsect2>
<refsect2 id="gdk-threads-leave" role="function">
<title>gdk_threads_leave ()</title>
<indexterm zone="gdk-threads-leave"><primary sortas="threads_leave">gdk_threads_leave</primary></indexterm><programlisting><link linkend="void">void</link>                gdk_threads_leave                   (void);</programlisting>
<para>
Leaves a critical region begun with <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>.
</para></refsect2>
<refsect2 id="gdk-threads-mutex" role="variable" condition="deprecated:">
<title>gdk_threads_mutex</title>
<indexterm zone="gdk-threads-mutex" role="deprecated"><primary sortas="threads_mutex">gdk_threads_mutex</primary></indexterm><programlisting>extern GMutex *gdk_threads_mutex; /* private */
</programlisting>
<warning><para><literal>gdk_threads_mutex</literal> is deprecated and should not be used in newly-written code.</para></warning>
<para>
The <link linkend="GMutex"><type>GMutex</type></link> used to implement the critical region for
<link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>/<link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link>.
</para></refsect2>
<refsect2 id="gdk-threads-set-lock-functions" role="function" condition="since:2.4">
<title>gdk_threads_set_lock_functions ()</title>
<indexterm zone="gdk-threads-set-lock-functions" role="2.4"><primary sortas="threads_set_lock_functions">gdk_threads_set_lock_functions</primary></indexterm><programlisting><link linkend="void">void</link>                gdk_threads_set_lock_functions      (<link linkend="GCallback">GCallback</link> enter_fn,
                                                         <link linkend="GCallback">GCallback</link> leave_fn);</programlisting>
<para>
Allows the application to replace the standard method that
GDK uses to protect its data structures. Normally, GDK
creates a single <link linkend="GMutex"><type>GMutex</type></link> that is locked by <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link>,
and released by <link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link>; using this function an
application provides, instead, a function <parameter>enter_fn</parameter> that is
called by <link linkend="gdk-threads-enter"><function>gdk_threads_enter()</function></link> and a function <parameter>leave_fn</parameter> that is
called by <link linkend="gdk-threads-leave"><function>gdk_threads_leave()</function></link>.
</para>
<para>
The functions must provide at least same locking functionality
as the default implementation, but can also do extra application
specific processing.
</para>
<para>
As an example, consider an application that has its own recursive
lock that when held, holds the GTK+ lock as well. When GTK+ unlocks
the GTK+ lock when entering a recursive main loop, the application
must temporarily release its lock as well.
</para>
<para>
Most threaded GTK+ apps won't need to use this method.
</para>
<para>
This method must be called before <link linkend="gdk-threads-init"><function>gdk_threads_init()</function></link>, and cannot
be called multiple times.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>enter_fn</parameter>&#160;:</term>
<listitem><simpara>   function called to guard GDK
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>leave_fn</parameter>&#160;:</term>
<listitem><simpara> function called to release the guard
</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.4</para></refsect2>
<refsect2 id="gdk-threads-add-idle" role="function" condition="since:2.12">
<title>gdk_threads_add_idle ()</title>
<indexterm zone="gdk-threads-add-idle" role="2.12"><primary sortas="threads_add_idle">gdk_threads_add_idle</primary></indexterm><programlisting><link linkend="guint">guint</link>               gdk_threads_add_idle                (<link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data);</programlisting>
<para>
A wrapper for the common usage of <link linkend="gdk-threads-add-idle-full"><function>gdk_threads_add_idle_full()</function></link> 
assigning the default priority, <link linkend="G-PRIORITY-DEFAULT-IDLE--CAPS"><type>G_PRIORITY_DEFAULT_IDLE</type></link>.
</para>
<para>
See <link linkend="gdk-threads-add-idle-full"><function>gdk_threads_add_idle_full()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>function</parameter>&#160;:</term>
<listitem><simpara> function to call
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>     data to pass to <parameter>function</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the ID (greater than 0) of the event source.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.12</para></refsect2>
<refsect2 id="gdk-threads-add-idle-full" role="function" condition="since:2.12">
<title>gdk_threads_add_idle_full ()</title>
<indexterm zone="gdk-threads-add-idle-full" role="2.12"><primary sortas="threads_add_idle_full">gdk_threads_add_idle_full</primary></indexterm><programlisting><link linkend="guint">guint</link>               gdk_threads_add_idle_full           (<link linkend="gint">gint</link> priority,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data,
                                                         <link linkend="GDestroyNotify">GDestroyNotify</link> notify);</programlisting>
<para>
Adds a function to be called whenever there are no higher priority
events pending.  If the function returns <link linkend="FALSE--CAPS"><literal>FALSE</literal></link> it is automatically
removed from the list of event sources and will not be called again.
</para>
<para>
This variant of <link linkend="g-idle-add-full"><function>g_idle_add_full()</function></link> calls <parameter>function</parameter> with the GDK lock
held. It can be thought of a MT-safe version for GTK+ widgets for the 
following use case, where you have to worry about <link linkend="idle-callback"><function>idle_callback()</function></link>
running in thread A and accessing <parameter>self</parameter> after it has been finalized
in thread B:
</para>
<para>
<informalexample><programlisting>
static gboolean
idle_callback (gpointer data)
{
   /&ast; gdk_threads_enter(); would be needed for g_idle_add() &ast;/

   SomeWidget *self = data;
   /&ast; do stuff with self &ast;/

   self->idle_id = 0;

   /&ast; gdk_threads_leave(); would be needed for g_idle_add() &ast;/
   return FALSE;
}

static void
some_widget_do_stuff_later (SomeWidget *self)
{
   self->idle_id = gdk_threads_add_idle (idle_callback, self)
   /&ast; using g_idle_add() here would require thread protection in the callback &ast;/
}

static void
some_widget_finalize (GObject *object)
{
   SomeWidget *self = SOME_WIDGET (object);
   if (self->idle_id)
     g_source_remove (self->idle_id);
   G_OBJECT_CLASS (parent_class)->finalize (object);
}
</programlisting></informalexample></para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>priority</parameter>&#160;:</term>
<listitem><simpara> the priority of the idle source. Typically this will be in the
           range btweeen <link linkend="G-PRIORITY-DEFAULT-IDLE--CAPS"><type>G_PRIORITY_DEFAULT_IDLE</type></link> and <link linkend="G-PRIORITY-HIGH-IDLE--CAPS"><type>G_PRIORITY_HIGH_IDLE</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>function</parameter>&#160;:</term>
<listitem><simpara> function to call
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>     data to pass to <parameter>function</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>notify</parameter>&#160;:</term>
<listitem><simpara>   function to call when the idle is removed, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the ID (greater than 0) of the event source.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.12</para></refsect2>
<refsect2 id="gdk-threads-add-timeout" role="function" condition="since:2.12">
<title>gdk_threads_add_timeout ()</title>
<indexterm zone="gdk-threads-add-timeout" role="2.12"><primary sortas="threads_add_timeout">gdk_threads_add_timeout</primary></indexterm><programlisting><link linkend="guint">guint</link>               gdk_threads_add_timeout             (<link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data);</programlisting>
<para>
A wrapper for the common usage of <link linkend="gdk-threads-add-timeout-full"><function>gdk_threads_add_timeout_full()</function></link> 
assigning the default priority, <link linkend="G-PRIORITY-DEFAULT--CAPS"><type>G_PRIORITY_DEFAULT</type></link>.
</para>
<para>
See <link linkend="gdk-threads-add-timeout-full"><function>gdk_threads_add_timeout_full()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>interval</parameter>&#160;:</term>
<listitem><simpara> the time between calls to the function, in milliseconds
            (1/1000ths of a second)
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>function</parameter>&#160;:</term>
<listitem><simpara> function to call
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>     data to pass to <parameter>function</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the ID (greater than 0) of the event source.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.12</para></refsect2>
<refsect2 id="gdk-threads-add-timeout-full" role="function" condition="since:2.12">
<title>gdk_threads_add_timeout_full ()</title>
<indexterm zone="gdk-threads-add-timeout-full" role="2.12"><primary sortas="threads_add_timeout_full">gdk_threads_add_timeout_full</primary></indexterm><programlisting><link linkend="guint">guint</link>               gdk_threads_add_timeout_full        (<link linkend="gint">gint</link> priority,
                                                         <link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data,
                                                         <link linkend="GDestroyNotify">GDestroyNotify</link> notify);</programlisting>
<para>
Sets a function to be called at regular intervals holding the GDK lock,
with the given priority.  The function is called repeatedly until it 
returns <link linkend="FALSE--CAPS"><literal>FALSE</literal></link>, at which point the timeout is automatically destroyed 
and the function will not be called again.  The <parameter>notify</parameter> function is
called when the timeout is destroyed.  The first call to the
function will be at the end of the first <parameter>interval</parameter>.
</para>
<para>
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).
</para>
<para>
This variant of <link linkend="g-timeout-add-full"><function>g_timeout_add_full()</function></link> can be thought of a MT-safe version 
for GTK+ widgets for the following use case:
</para>
<para>
<informalexample><programlisting>
static gboolean timeout_callback (gpointer data)
{
   SomeWidget *self = data;
   
   /&ast; do stuff with self &ast;/
   
   self->timeout_id = 0;
   
   return FALSE;
}
 
static void some_widget_do_stuff_later (SomeWidget *self)
{
   self->timeout_id = g_timeout_add (timeout_callback, self)
}
 
static void some_widget_finalize (GObject *object)
{
   SomeWidget *self = SOME_WIDGET (object);
   
   if (self->timeout_id)
     g_source_remove (self->timeout_id);
   
   G_OBJECT_CLASS (parent_class)->finalize (object);
}
</programlisting></informalexample></para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>priority</parameter>&#160;:</term>
<listitem><simpara> the priority of the timeout source. Typically this will be in the
           range between <link linkend="G-PRIORITY-DEFAULT-IDLE--CAPS"><type>G_PRIORITY_DEFAULT_IDLE</type></link> and <link linkend="G-PRIORITY-HIGH-IDLE--CAPS"><type>G_PRIORITY_HIGH_IDLE</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>interval</parameter>&#160;:</term>
<listitem><simpara> the time between calls to the function, in milliseconds
            (1/1000ths of a second)
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>function</parameter>&#160;:</term>
<listitem><simpara> function to call
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>     data to pass to <parameter>function</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>notify</parameter>&#160;:</term>
<listitem><simpara>   function to call when the timeout is removed, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the ID (greater than 0) of the event source.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.12</para></refsect2>
<refsect2 id="gdk-threads-add-timeout-seconds" role="function" condition="since:2.14">
<title>gdk_threads_add_timeout_seconds ()</title>
<indexterm zone="gdk-threads-add-timeout-seconds" role="2.14"><primary sortas="threads_add_timeout_seconds">gdk_threads_add_timeout_seconds</primary></indexterm><programlisting><link linkend="guint">guint</link>               gdk_threads_add_timeout_seconds     (<link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data);</programlisting>
<para>
A wrapper for the common usage of <link linkend="gdk-threads-add-timeout-seconds-full"><function>gdk_threads_add_timeout_seconds_full()</function></link> 
assigning the default priority, <link linkend="G-PRIORITY-DEFAULT--CAPS"><type>G_PRIORITY_DEFAULT</type></link>.
</para>
<para>
For details, see <link linkend="gdk-threads-add-timeout-full"><function>gdk_threads_add_timeout_full()</function></link>.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>interval</parameter>&#160;:</term>
<listitem><simpara> the time between calls to the function, in seconds
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>function</parameter>&#160;:</term>
<listitem><simpara> function to call
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>     data to pass to <parameter>function</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the ID (greater than 0) of the event source.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.14</para></refsect2>
<refsect2 id="gdk-threads-add-timeout-seconds-full" role="function" condition="since:2.14">
<title>gdk_threads_add_timeout_seconds_full ()</title>
<indexterm zone="gdk-threads-add-timeout-seconds-full" role="2.14"><primary sortas="threads_add_timeout_seconds_full">gdk_threads_add_timeout_seconds_full</primary></indexterm><programlisting><link linkend="guint">guint</link>               gdk_threads_add_timeout_seconds_full
                                                        (<link linkend="gint">gint</link> priority,
                                                         <link linkend="guint">guint</link> interval,
                                                         <link linkend="GSourceFunc">GSourceFunc</link> function,
                                                         <link linkend="gpointer">gpointer</link> data,
                                                         <link linkend="GDestroyNotify">GDestroyNotify</link> notify);</programlisting>
<para>
A variant of <link linkend="gdk-threads-add-timout-full"><function>gdk_threads_add_timout_full()</function></link> with second-granularity.
See <link linkend="g-timeout-add-seconds-full"><function>g_timeout_add_seconds_full()</function></link> for a discussion of why it is
a good idea to use this function if you don't need finer granularity.</para>
<para>
</para><variablelist role="params">
<varlistentry><term><parameter>priority</parameter>&#160;:</term>
<listitem><simpara> the priority of the timeout source. Typically this will be in the
           range between <link linkend="G-PRIORITY-DEFAULT-IDLE--CAPS"><type>G_PRIORITY_DEFAULT_IDLE</type></link> and <link linkend="G-PRIORITY-HIGH-IDLE--CAPS"><type>G_PRIORITY_HIGH_IDLE</type></link>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>interval</parameter>&#160;:</term>
<listitem><simpara> the time between calls to the function, in seconds
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>function</parameter>&#160;:</term>
<listitem><simpara> function to call
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>     data to pass to <parameter>function</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>notify</parameter>&#160;:</term>
<listitem><simpara>   function to call when the timeout is removed, or <link linkend="NULL--CAPS"><literal>NULL</literal></link>
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the ID (greater than 0) of the event source.

</simpara></listitem></varlistentry>
</variablelist><para role="since">Since 2.14</para></refsect2>

</refsect1>




</refentry>
