Glib::Dispatcher and data transfer into main loop



Hello,

I'm using a Glib::Dispatcher to dispatch data from a thread into my
main loop. This is my code:

Glib::Dispatcher dispatchSignal;

... 
  // cMan is a thread...
  cMan.signalFinish.connect (sigc::mem_fun (this, &IconBrowser::onFinishWrapper));
  dispatchSignal.connect(sigc::mem_fun(*this, &IconBrowser::onFinish));
...

void IconBrowser::onFinishWrapper (const CacheManagerFinishEvent &dlFinishEvent)
{
  cout << "IconBrowser:: Download of '" << dlFinishEvent.getFilename () 
       << "' (" << (float) dlFinishEvent.getMemory ().size () / (1024.0) << " kb ) finished!" << endl;
  
  m_addIconMutex.lock ();

  m_iconData.filename = dlFinishEvent.getFilename ();
  m_iconData.description = dlFinishEvent.getDescription ();
  m_iconData.href = dlFinishEvent.getHref ();
  
  dispatchSignal ();
  
  m_addIconMutex.unlock ();
}

void IconBrowser::onFinish ()
{
  Global &global = Global::instance ();
  Gtk::ProgressBar *loadProgressBar = global.getProgressBar ();
  
  m_addIconMutex.lock ();
  
  addIconEntry (m_iconData.filename, 
                Glib::path_get_basename (m_iconData.description), 
                m_iconData.href, 128, 128);
  
  ++iconCount;
  
  cout << "progress: " << progressStatus << endl;
  loadProgressBar->set_fraction (progressStatus);
  progressStatus += progressStep;
  
  if (iconCount >= wpSize)
  {
    iconCount = 0;
    progressStatus = 0;
    loadProgressBar->set_fraction (iconCount);
  }
  
  m_addIconMutex.unlock ();
}

This works in most cases. But sometimes The thread calls multiple times onFinishWrapper()
before the main loop called each dispatched onFinish(). So the data saved in m_iconData
is not correct in some cases.

Any ideas how to solve that problem? I've seen no way to give data to the dispatchSignal()
call. So I need a member variable to transfer data from the thread called function to the
dispatched function. I could think of a queue that saves all the incoming data from the 
thread for later use in the dispatched function. Do you think that's the best way to do it?
Any other ideas?

regards
Andreas


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