Re: gdk_threads_leave in gtk callback ?



Vladimir wrote:

Thanks a lot for comprehensive explanations.

There are two uses of GTK+ in non-main threads:
1. error reporting via message boxes and 
2. adding text (which comes from network) to GUI windows.

I can do both of it in main thread but this involves copying text to
dynamically allocated buffers.

There shouldn't be any additional buffer copying required just because
dealing with it moves from one thread to another. You know, all threads
have the same access to all program data.

More reasonable would probably be to copy just pointers to the buffers
between threads. For instance, provide just two pointers like these:

gchar *text_for_msg_box;
gchar *text_to_add;

Most of the time they'll be NULL. The main thread occasionally checks
whether they have become non-NULL, for instance within an idle or
timeout handler. If they have, then that handler (in the main thread)
either displays a message box or adds the text to your windows. When
finished, it clears the pointers again. They would be set by the other
thread(s) only, of course.

Slightly more sophisticated buffer management might be necessary if you
expect new data to come in and be handled by the other thread while for
instance a message box is being open. However, necessary measures are
the same, no matter whether you display the message box from within the
main thread or from any other thread.

BTW, can I use g_idle_add in non-main thread without locking ?

I am not sure. The documentation is not clear about this and I havn't
had the time to examine the sources of that yet. To be safe, you should
wrap a simple GStaticMutex lock around the call.



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