Multiple threads and drawing



Hello. I'm trying to draw the GUI in gtkmm through a dispatcher, since
I cannot do it from multiple threads.
The program behaves strangely. Sometimes it shows the images, others
get stuck and other it does not show
the table at all.

I have multiple threads that calculate thumbnails of images. The
program creates a Table to contain the images.
Once an image is available (which is calculated by worker threads) , a
signal containing the image is sent from that thread
so that it can be put inside the table. Inside the slot Ithat handles
a new thumbnail available I attach the image  to the table and then
call the dispatcher(),
which calls show_all().
Is this correct?

The (simplified) code is something like this:

class GUI : public Gtk::Window
{
private:
   Glib::Dispatcher d_;
   Gtk::Table * table_;
   Gtk::Button buttonprocessimages_;
   Gtk::Label l_;
   ThreadPool & pool_;

   void onImageAvailable(...)
   {
       table_->attach(*img, row, col,...);
       d_();

   }

   void redraw()
   {
      show_all();
   }
   void processImages()
   {
       table_ = Gtk::Manage(new Gtk::Table(........
       pool_.set_max_threads(n);
       send_work_to_process...
       label_.set_text("");

       buttonprocessimages_.set_sensitive(false);
   }
public:
   GUI(Glib::ThreadPool & pool) : pool_(pool)
   {
     buttonprocessimages_.connect(sigc::mem_fun(*this, &GUI::processImages));
     signal_image_available.connect(sigc::mem_fun(*this,
&GUI::onImageAvailable));
      d_.connect(sigc::mem_fun(*this, &GUI::redraw));

   }
};


int main(int argc, char * argv[])
{
   Gtk::Main m(argc, argv);
   Glib::thread_init();

   Glib::ThreadPool pool;

   GUI gui(pool);
   pool_.set_max_threads(2);

   Gtk::Main::run(gui);

   return 0;
}

Can help, please? Maybe I'm drawing implicitly when I use
label.set_text() or table_.attach() from a non-GUI thread?
Thanks for you time.


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