Re: [gtk-list] A basic idea of threading GTK apps



I am doing a complex multithread app with Gtk-- and pthreads. The
approach I've taken to threading has been to create a thread that does
all the UI work, and nothing else. Any other thread who wants
something done by the UI thread uses this interface:

	  enum UIRequest {
	        Check_ErrorQueue,
		Quit,
		...
          }

          void
	  tell_ui (UIRequest req)
	  
	  { 
	           char c = (char) req;

		   write (ui_signal_pipe[writable], &c, 1);
          }


The signal pipe is set up before the UI thread starts up. The
read-side of the pipe is added to the selected inputs with gtk_input_add().
This way, all UI requests are handled synchronously, as X requires.

the UI thread just runs this callback whenever something shows up on
the signal pipe:

          signal_pipe_callback (int fd, GdkInputCondition cond)

	  {
	      ...

	      char c;

	      read (ui_signal_pipe[readable], &c, 1);

	      switch ((UIRequest) c) {
	      case Check_ErrorQueue:
	            check_error_queue ();
		    break;
	      case Quit:
	            /* do whatever is necessary */
	      ...
	      }
	  }


This works fine for. I have 4-5 threads all writing to the error log
(which is just an in-memory queue) at the same time, and the UI thread
handles pop-ups of the viewer for this just perfectly.

--p



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