[gtkmm] threads



And now some threads questions...

I have a gui with a toggle button which must start a new thread when
clicked active, and stop the computation when clicked inactive. Here's how
I'm doing this so far. Could someone confirm if this is right?

void window1::on_togglebutton1_toggled()
{
    if(this->togglebutton1->get_active()){
	std::cout << "not done" << std::endl;
	done=0;               // bool, member of window1

	Glib::Thread *const producer = Glib::Thread::create(
	    SigC::slot_class(*this, &window1::work_function), true);
    }
    else{
	std::cout << "done" << std::endl;
	done=1;
    }
}

void window1::work_function()
{
    unsigned int i=0;
    while(!done)
	if(i%50==1)
	    std::cout << "drawing now" << std::endl;
}

bool window1::on_drawingarea1_key_press_event(GdkEventKey *ev)
{
    switch(ev->keyval){
    case GDK_Q:
    case GDK_q:
	std::cout << "exit" << std::endl;
	Gtk::Main::quit();
 	break;
    }

    return 1;
}


With a blank stub for on_togglebutton1_toggled() (i.e. if I don't create
the thread), the application quits when I press q in the drawing area, as
I want it to.

With the code provided above, the thread starts/stops when I toggle the
start/stop button, but the application no longer quits when I press q. It
does quit, however, (even with the thread) if I connect the window itself
(not the drawing area) to the key_press_event. What am I doing wrong? Is
the above code fundamentally flawed?

Thanks!





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