[gtkmm] Threading Errors



Hi

I have had success updating a progress bar though two threads using a
Gtk::Dispatcher. However, I need to get the same results using
gdk_threads_enter / leave.

I have attached a test program that creates a window in the main GUI
thread with a progress bar. This progress bar is then updated from a
separate thread. I have tried placing gdk_threads_enter and leave
functions around the Gtk functions of the progress bar. However, I must
be missing something as I keep getting

Xlib: unexpected async reply (sequence 0xac)!

I would be grateful for any advice on what I doing wrong.

Thanks

Glenn Pierce
#include <glibmm.h>

#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/progressbar.h>

#include <iostream>

class ProgressBarDialog : public Gtk::Window
{
  public:
    ProgressBarDialog();
    void set_fraction(double progress);

  private:

    Gtk::ProgressBar *progress_bar;
};

ProgressBarDialog::ProgressBarDialog() : Gtk::Window(Gtk::WINDOW_TOPLEVEL)
{
    gdk_threads_enter();

    this->set_default_size(400, 50);

    // Create the ProgressBar
    progress_bar = new Gtk::ProgressBar();
    this->set_fraction(0);

    add(*progress_bar);

    gdk_flush();
    gdk_threads_leave();
}

void ProgressBarDialog::set_fraction(double progress)
{
    gdk_threads_enter();

    this->progress_bar->set_fraction( progress );

    gdk_flush();
    gdk_threads_leave();
}

void launch_thread(ProgressBarDialog *progress)
{
    Glib::Rand rand;
    int usecs = 50;

    for(int i = 0; i < 1000; ++i)
    {
      Glib::usleep(usecs);

      progress->set_fraction((i / 1000));
    }
}

int main(int argc, char** argv)
{
    Glib::thread_init();
    //gdk_threads_init();

    Gtk::Main main_instance (&argc, &argv);

    //gdk_threads_enter();

    ProgressBarDialog *dialog = new ProgressBarDialog();
    dialog->show_all();

    //gdk_flush();
    //gdk_threads_leave();

    Glib::Thread::create( sigc::bind<ProgressBarDialog *>( sigc::ptr_fun(&launch_thread), dialog), true);

    gdk_threads_enter();
    Gtk::Main::run(*dialog);
    gdk_threads_leave();

    delete dialog;

    return 0;
}


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