Re: Threads and main-loops



Russell Shaw wrote:

Hi,

If i make multiple loops with:

  GMainLoop *loop1=g_main_loop_new(context1,TRUE);
  GMainLoop *loop2=g_main_loop_new(context2,TRUE);
  GMainLoop *loop3=g_main_loop_new(context3,TRUE);
  g_main_loop_run(loop1);
  g_main_loop_run(loop2);
  g_main_loop_run(loop3);

Are all these running in a single gtk thread? If so, can their
callbacks call GUI widgets without thread-locking problems?

i think you're misunderstanding something here. the first g_main_loop_run() call will block until loop1 is exited (usually by calling g_main_loop_quit(loop1) in some callback or timeout function). then loop2 will run, and after it exits, loop3 will run.

i'm not sure exactly what you're trying to do here, but i suspect this isn't what you're looking for. gtk itself is not multithreaded. put simply, it uses an event-loop mechanism. callbacks run in the main thread. everything runs in a single thread, unless you explicitly create other threads (either via the GThread interface, or using raw pthread calls; GThread is just a wrapper around pthreads on linux). if you need separate threads, you either can only do gtk operations in the main thread, or you need to initialise gdk's threads support and use gdk_threads_enter() and gdk_threads_leave() to protect gtk calls in the other threads. there is plenty of information in tutorials and whatnot on how to do this, so i won't repeat it here.

   -brian



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