Re: Clarification of GTK/GDK locking pre GTK 4.0



hi;

On 14 September 2012 16:02, richard boaz <ivor boaz gmail com> wrote:
>>
>> if you're only using GTK+ for an application, and you're not using
>> threads directly, then you can skip the gdk_threads_add_* API, and use
>> the GMainLoop API for idle and timeout sources.
>>
>
> not using threads directly?  what's an indirect thread?

it means *you* may not be using threads, but one of your dependencies
does, or might do under library-specific circumstances.

> i too was confused that it seems g_idle_add() is not recommended in this
> discussion.  but i use it everywhere in my code to ensure that
> g_idle_add()ed callbacks execute as part of the main loop, so to guarantee
> single concurrency, alleviating the need for a lock, whatever its name (and
> it works just grand).
>
> perhaps better explained by example situations?  which situation disallows
> g_idle_add(), requiring GDK locks/etc.?  and when is g_idle_add() 100%
> thread-safe?

GDK has to be backwards compatible for the duration of the 3.x API
series, so applications and libraries can still use
gdk_threads_enter() and gdk_threads_leave() inside a thread to mark
the critical section. this means that the callback for g_idle_add() or
g_timeout_add() even if it has been scheduled for execution under the
default GMainContext, may still happen concurrently with some threaded
code under the same GMainContext. that's what the GDK lock is supposed
to prevent. this means that the idle or timeout callback must also be
invoked under the GDK lock; the GDK lock is not known by GLib and the
GMainLoop, so GDK has wrappers for scheduling idle and timeout
handlers, in the form of the gdk_threads_add_* family of functions.

if you are writing a library it's mandatory to use gdk_threads_add_*
in place of g_idle_add() and g_timeout_add() to avoid applications
exploding because they are using enter()/leave() inside a thread; if
you are writing an application you need to be sure that the libraries
you're depending on are not going to call enter()/leave() inside a
thread.

if either conditions outlined above do not apply to you, then you can
keep using g_idle_add() and g_timeout_add() without having to about
the GDK lock, because you know that your code and your dependencies
will do the right thing, and will use GDK and GTK+ from a single
thread.

hopefully this clarified the issue.

ciao,
 Emmanuele.

-- 
W: http://www.emmanuelebassi.name
B: http://blogs.gnome.org/ebassi/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]