Re: Multi-threaded Text Insertion into a TextView



DESCOMBES Thierry wrote:

Hello,

I have several POSIX threads trying to insert text into a Textview. So,
I've used a POSIX mutex to be sure that they are inserting text into the
TextBuffer one after the other.
[snip]

ugh... well, gtk and threading isn't all that fun. what you need to do is, at the beginning of your main(), right before you call gtk_init(), call g_thread_init(NULL) and gdk_threads_init(). then, surround your call to gtk_main() with:

gdk_threads_enter();
/* actually, you probably want to put your thread creation stuff in here too. */
gtk_main();
gdk_threads_leave();

then, in any thread but the main thread, whenever you call _any_ gtk or gdk function, surround that code block with gdk_threads_enter()/gdk_threads_leave(). also do this for any functions that are invoked via g_idle_add() or g_timeout_add().

your other (and somewhat better, IMHO) option is to not do any of the stuff i just mentioned, and use a GAsyncQueue to communicate between the other threads and the main thread. the main thread can do the actual adding of the text to the textview widget. if you use this method, you aren't allowed to call any gtk or gdk functions from anything but the main thread (well, there are possibly a few that are ok, but better safe than sorry).

of course, you're using gtkmm, so you'd want to use the C++ equivalents to these functions/objects.

there are probably several resources on the web regarding gtk and multithreaded apps; i suggest you find and read them.

   -brian



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