<ThreadSafetyStatement>Gtk# is thread aware, but not thread safe; See the <linklocation="node:gtk-sharp/programming/threads">Gtk# Thread Programming</link> for details.</ThreadSafetyStatement>
<para>Functions for using GDK in multi-threaded programs</para>
<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 GHashTable from multiple threads.</para>
<para>Gtk# is "thread aware" but not thread safe ? it provides a global lock controlled by <seecref="M:Gdk.Threads.Enter()"/>/<seecref="M:Gdk.Threads.Leave()"/> which protects all use of Gtk. That is, only one thread can use Gtk at any given time.</para>
<para>You must call <seecref="M:Gdk.Threads.Init()"/> before executing any other Gtk or Gdk functions in a threaded Gtk# program.</para>
<para>Idles, timeouts, and input functions 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 <seecref="M:Gdk.Threads.Enter()"/>/<seecref="M:Gdk.Threads.Leave()"/> pair. (However, signals are still executed within the main Gtk lock.)</para>
<para>In particular, this means, if you are writing widgets that might be used in threaded programs, you must 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 <seecref="M:Gdk.Threads.Enter()"/>/<seecref="M:Gdk.Threads.Leave()"/> pair.
Before calling <seecref="M:Gdk.Threads.Leave()"/> from a thread other than your main thread, you probably want to call gdk_flush() to send all pending commands to the windowing system. (The reason you do not 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:
<MemberSignatureLanguage="ILAsm"Value=".method public static hidebysig void Init() cil managed"/>
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters/>
<Docs>
<summary>This call must be made before any use of the main loop from Gtk#; to be safe, call it before <seecref="M:Gtk.Application.Init()"/>. It must also be preceded by a call to <seecref="M:GLib.Thread.Init()"/> if GLib threading has not yet been initialized.</summary>
<remarks>Initializes <seecref="N:Gdk"/> so that it can be used from multiple threads in conjunction with <seecref="M:Gdk.Threads.Enter()"/> and <seecref="M:Gdk.Threads.Leave()"/>.</remarks>