[gtkmm] thread priorities are reversed!




Hello. I created this program where you can start threads with different
priorities.  I discovered that what gtkmm this is the low priority beats
the  so-called  urgent-priority.  Somewhere  someone  is  mistaking  low
priority values for low priorities!...

Has anybody seen that also? Is that a bug in GTKmm?

I'm sending the program attached. It has to be run as root.

Sorry if I'm getting off-topic.

-- 
Nicolau Werneck <nwerneck cefala org>         9F99 25AB E47E 8724 2F71
http://cefala.org/~nwerneck                   EA40 DC23 42CE 6B76 B07F
"The great tragedy of science -- the slaying of a beautiful hypothesis by an ugly fact. "
-- Thomas Huxley

#define __user

//#include <glib.h>
#include <glibmm.h>
#include <libglademm/xml.h>
#include <libglademm.h>
#include <gtkmm.h>
#include <iostream>
#include <sstream>

extern "C" {
#include <stdio.h>
#include <unistd.h>

#include <stddef.h>
#include <stdlib.h>

#include <math.h>

#include <signal.h>

#include <sys/types.h>
#include <sys/wait.h>

} using namespace std;

class Processo : public SigC::Object {

private:
	Glib::Dispatcher signal_increment_;
	bool BoolAcabar;

public:
	Gtk::ProgressBar & Barra;
	Gtk::Combo & Com;
	Glib::Thread * pThread;
	Glib::RefPtr<Gdk::Window> & pWin;

	float f;


	Processo(Glib::RefPtr<Gdk::Window>cons_pWin, Gtk::ProgressBar & cons_Barra, Gtk::Combo & cons_Com)
			: pWin(cons_pWin), Barra(cons_Barra), Com(cons_Com)
	{
		// Criação da estrutura da thread
		Barra.set_fraction(0.0);
		signal_increment_.connect(SigC::slot(*this, &Processo::anda_barra));
	}

	void iniciar() {
		Com.set_sensitive(false);
		Glib::ustring PrioridadeSelecionada;
		Glib::ThreadPriority PrioridadeValor;

		Gtk::Entry* pEntry = Com.get_entry();
		if(pEntry) PrioridadeSelecionada = pEntry->get_text();

		PrioridadeValor = (PrioridadeSelecionada == "URGENTE")?Glib::THREAD_PRIORITY_URGENT:	\
		                  ((PrioridadeSelecionada == "ALTA")?Glib::THREAD_PRIORITY_HIGH:	\
		                   ((PrioridadeSelecionada == "NORMAL")?Glib::THREAD_PRIORITY_NORMAL:	\
		                    Glib::THREAD_PRIORITY_LOW));
		BoolAcabar=false;
//		pThread = Glib::Thread::create(SigC::slot(*this, &Processo::funcao), false);
		cout << "[" << this << "] iniciar com prioridade " << PrioridadeSelecionada << " (" << PrioridadeValor << ")" << endl;
		pThread = Glib::Thread::create(SigC::slot(*this, &Processo::funcao), 0, false, true, PrioridadeValor);
	}

	void parar() {
		cout << "[" << this << "] parar" << endl;
		BoolAcabar = true;
	}

	void anda_barra() {
		Barra.set_fraction(f);
	}

	void funcao () {
		Gdk::Rectangle rect;
		int x,y,width,height,depth;

		float g;
		unsigned int i;
		for (f = 0; f <= 1.0 && !BoolAcabar; f+=0.005, signal_increment_() )
			for (i=0,g=1.5; i<200000 && !BoolAcabar; i++, g*=f*g/(f-g)*sin(g*i) );  // stupid operations to waste time
	}

};


class BotaoMestre : public SigC::Object {

protected:
	SigC::Connection ConBot;
	Gtk::Button & Bot;
	Processo & Proc1;
	Processo & Proc2;

public:
	BotaoMestre(Gtk::Button& cons_Bot, Processo& cons_Proc1, Processo& cons_Proc2) :
			Bot(cons_Bot), Proc1(cons_Proc1), Proc2(cons_Proc2)
	{
		ConBot = Bot.signal_clicked().connect( SigC::slot(*this, &BotaoMestre::ao_apertar_executar) );
	}

	virtual ~BotaoMestre() {};

	void ao_apertar_executar() {
		ConBot.disconnect();
		Proc1.iniciar();
		Proc2.iniciar();
		Bot.set_label("gtk-stop");
		ConBot = Bot.signal_clicked().connect( SigC::slot(*this, &BotaoMestre::ao_apertar_parar) );
	}

	void ao_apertar_parar() {
		ConBot.disconnect();
		Proc1.parar();
		Proc2.parar();
		Bot.set_label("gtk-execute");
		ConBot = Bot.signal_clicked().connect( SigC::slot(*this, &BotaoMestre::ao_apertar_executar) );
	}
};


int main(int argc, char **argv)
{
	if (!Glib::thread_supported()) {
		Glib::thread_init();
	} else {
		exit(1);
	}

	Gtk::Main kit(argc, argv);

	//Load the Glade file and instiate its widgets:
	Glib::RefPtr < Gnome::Glade::Xml > refXml;
	try {
		refXml = Gnome::Glade::Xml::create("exemplo03.glade");
	}
	catch(const Gnome::Glade::XmlError & ex) {
		std::cerr << ex.what() << std::endl;
		return 1;
	}

	//Get the Glade-instantiated Dialog:
	Gtk::Window * pJan = 0;
	Gtk::ProgressBar * pBar1 = 0;
	Gtk::ProgressBar * pBar2 = 0;
	Gtk::Combo * pCom1 = 0;
	Gtk::Combo * pCom2 = 0;
	Gtk::Button * pBot = 0;

	refXml->get_widget("window1", pJan);
	refXml->get_widget("progressbar1", pBar1);
	refXml->get_widget("progressbar2", pBar2);
	refXml->get_widget("combo1", pCom1);
	refXml->get_widget("combo2", pCom2);
	refXml->get_widget("button1", pBot);

	if ( pJan && pBar1 && pBar2 && pCom1 && pCom2 && pBot )
	{
		Processo Proc1(pJan->get_frame(), *pBar1, *pCom1);
		Processo Proc2(pJan->get_frame(), *pBar2, *pCom2);

		BotaoMestre BM(*pBot, Proc1, Proc2);

		//		pBot->signal_clicked().connect( SigC::slot(*this, &DerivedDialog::on_button_quit) );
		kit.run(*pJan);
	}
	return 0;
}
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd";>

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">window1</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="border_width">8</property>
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
	<widget class="GtkFrame" id="frame1">
	  <property name="border_width">8</property>
	  <property name="visible">True</property>
	  <property name="label_xalign">0</property>
	  <property name="label_yalign">0.5</property>
	  <property name="shadow_type">GTK_SHADOW_ETCHED_IN</property>

	  <child>
	    <widget class="GtkVBox" id="vbox2">
	      <property name="border_width">15</property>
	      <property name="visible">True</property>
	      <property name="homogeneous">True</property>
	      <property name="spacing">0</property>

	      <child>
		<widget class="GtkProgressBar" id="progressbar1">
		  <property name="visible">True</property>
		  <property name="orientation">GTK_PROGRESS_LEFT_TO_RIGHT</property>
		  <property name="fraction">0</property>
		  <property name="pulse_step">0.1</property>
		  <property name="text" translatable="yes">PROCESSO 1</property>
		</widget>
		<packing>
		  <property name="padding">5</property>
		  <property name="expand">True</property>
		  <property name="fill">False</property>
		</packing>
	      </child>

	      <child>
		<widget class="GtkHBox" id="hbox2">
		  <property name="visible">True</property>
		  <property name="homogeneous">False</property>
		  <property name="spacing">0</property>

		  <child>
		    <widget class="GtkLabel" id="label3">
		      <property name="visible">True</property>
		      <property name="label" translatable="yes">Prioridade: </property>
		      <property name="use_underline">False</property>
		      <property name="use_markup">False</property>
		      <property name="justify">GTK_JUSTIFY_LEFT</property>
		      <property name="wrap">False</property>
		      <property name="selectable">False</property>
		      <property name="xalign">0.5</property>
		      <property name="yalign">0.5</property>
		      <property name="xpad">0</property>
		      <property name="ypad">0</property>
		    </widget>
		    <packing>
		      <property name="padding">0</property>
		      <property name="expand">False</property>
		      <property name="fill">False</property>
		    </packing>
		  </child>

		  <child>
		    <widget class="GtkCombo" id="combo2">
		      <property name="visible">True</property>
		      <property name="value_in_list">False</property>
		      <property name="allow_empty">True</property>
		      <property name="case_sensitive">False</property>
		      <property name="enable_arrow_keys">True</property>
		      <property name="enable_arrows_always">False</property>

		      <child internal-child="entry">
			<widget class="GtkEntry" id="combo-entry2">
			  <property name="visible">True</property>
			  <property name="can_focus">True</property>
			  <property name="editable">False</property>
			  <property name="visibility">True</property>
			  <property name="max_length">0</property>
			  <property name="text" translatable="yes"></property>
			  <property name="has_frame">True</property>
			  <property name="invisible_char" translatable="yes">*</property>
			  <property name="activates_default">False</property>
			</widget>
		      </child>

		      <child internal-child="list">
			<widget class="GtkList" id="combo-list2">
			  <property name="visible">True</property>
			  <property name="selection_mode">GTK_SELECTION_BROWSE</property>

			  <child>
			    <widget class="GtkListItem" id="listitem119">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">URGENTE</property>
			    </widget>
			  </child>

			  <child>
			    <widget class="GtkListItem" id="listitem120">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">NORMAL</property>
			    </widget>
			  </child>

			  <child>
			    <widget class="GtkListItem" id="listitem121">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">ALTA</property>
			    </widget>
			  </child>

			  <child>
			    <widget class="GtkListItem" id="listitem122">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">BAIXA</property>
			    </widget>
			  </child>
			</widget>
		      </child>
		    </widget>
		    <packing>
		      <property name="padding">0</property>
		      <property name="expand">True</property>
		      <property name="fill">True</property>
		    </packing>
		  </child>
		</widget>
		<packing>
		  <property name="padding">0</property>
		  <property name="expand">False</property>
		  <property name="fill">True</property>
		</packing>
	      </child>
	    </widget>
	  </child>

	  <child>
	    <widget class="GtkLabel" id="label1">
	      <property name="visible">True</property>
	      <property name="label" translatable="yes">Thread 1</property>
	      <property name="use_underline">False</property>
	      <property name="use_markup">False</property>
	      <property name="justify">GTK_JUSTIFY_LEFT</property>
	      <property name="wrap">False</property>
	      <property name="selectable">False</property>
	      <property name="xalign">0.5</property>
	      <property name="yalign">0.5</property>
	      <property name="xpad">0</property>
	      <property name="ypad">0</property>
	    </widget>
	    <packing>
	      <property name="type">label_item</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>

      <child>
	<widget class="GtkFrame" id="frame2">
	  <property name="border_width">8</property>
	  <property name="visible">True</property>
	  <property name="label_xalign">0</property>
	  <property name="label_yalign">0.5</property>
	  <property name="shadow_type">GTK_SHADOW_ETCHED_IN</property>

	  <child>
	    <widget class="GtkVBox" id="vbox3">
	      <property name="border_width">15</property>
	      <property name="visible">True</property>
	      <property name="homogeneous">True</property>
	      <property name="spacing">0</property>

	      <child>
		<widget class="GtkProgressBar" id="progressbar2">
		  <property name="visible">True</property>
		  <property name="orientation">GTK_PROGRESS_LEFT_TO_RIGHT</property>
		  <property name="fraction">0</property>
		  <property name="pulse_step">0</property>
		  <property name="text" translatable="yes">PROCESSO 2</property>
		</widget>
		<packing>
		  <property name="padding">5</property>
		  <property name="expand">True</property>
		  <property name="fill">False</property>
		</packing>
	      </child>

	      <child>
		<widget class="GtkHBox" id="hbox1">
		  <property name="visible">True</property>
		  <property name="homogeneous">False</property>
		  <property name="spacing">0</property>

		  <child>
		    <widget class="GtkLabel" id="label4">
		      <property name="visible">True</property>
		      <property name="label" translatable="yes">Prioridade: </property>
		      <property name="use_underline">False</property>
		      <property name="use_markup">False</property>
		      <property name="justify">GTK_JUSTIFY_LEFT</property>
		      <property name="wrap">False</property>
		      <property name="selectable">False</property>
		      <property name="xalign">0.5</property>
		      <property name="yalign">0.5</property>
		      <property name="xpad">0</property>
		      <property name="ypad">0</property>
		    </widget>
		    <packing>
		      <property name="padding">0</property>
		      <property name="expand">False</property>
		      <property name="fill">False</property>
		    </packing>
		  </child>

		  <child>
		    <widget class="GtkCombo" id="combo1">
		      <property name="visible">True</property>
		      <property name="value_in_list">False</property>
		      <property name="allow_empty">True</property>
		      <property name="case_sensitive">False</property>
		      <property name="enable_arrow_keys">True</property>
		      <property name="enable_arrows_always">False</property>

		      <child internal-child="entry">
			<widget class="GtkEntry" id="combo-entry1">
			  <property name="visible">True</property>
			  <property name="can_focus">True</property>
			  <property name="editable">False</property>
			  <property name="visibility">True</property>
			  <property name="max_length">0</property>
			  <property name="text" translatable="yes"></property>
			  <property name="has_frame">True</property>
			  <property name="invisible_char" translatable="yes">*</property>
			  <property name="activates_default">False</property>
			</widget>
		      </child>

		      <child internal-child="list">
			<widget class="GtkList" id="combo-list1">
			  <property name="visible">True</property>
			  <property name="selection_mode">GTK_SELECTION_BROWSE</property>

			  <child>
			    <widget class="GtkListItem" id="listitem115">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">URGENTE</property>
			    </widget>
			  </child>

			  <child>
			    <widget class="GtkListItem" id="listitem116">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">NORMAL</property>
			    </widget>
			  </child>

			  <child>
			    <widget class="GtkListItem" id="listitem117">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">ALTA</property>
			    </widget>
			  </child>

			  <child>
			    <widget class="GtkListItem" id="listitem118">
			      <property name="visible">True</property>
			      <property name="can_focus">True</property>
			      <property name="label" translatable="yes">BAIXA</property>
			    </widget>
			  </child>
			</widget>
		      </child>
		    </widget>
		    <packing>
		      <property name="padding">0</property>
		      <property name="expand">True</property>
		      <property name="fill">True</property>
		    </packing>
		  </child>
		</widget>
		<packing>
		  <property name="padding">0</property>
		  <property name="expand">False</property>
		  <property name="fill">True</property>
		</packing>
	      </child>
	    </widget>
	  </child>

	  <child>
	    <widget class="GtkLabel" id="label2">
	      <property name="visible">True</property>
	      <property name="label" translatable="yes">Thread 2</property>
	      <property name="use_underline">False</property>
	      <property name="use_markup">False</property>
	      <property name="justify">GTK_JUSTIFY_LEFT</property>
	      <property name="wrap">False</property>
	      <property name="selectable">False</property>
	      <property name="xalign">0.5</property>
	      <property name="yalign">0.5</property>
	      <property name="xpad">0</property>
	      <property name="ypad">0</property>
	    </widget>
	    <packing>
	      <property name="type">label_item</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">True</property>
	  <property name="fill">True</property>
	</packing>
      </child>

      <child>
	<widget class="GtkHBox" id="hbox3">
	  <property name="visible">True</property>
	  <property name="homogeneous">False</property>
	  <property name="spacing">0</property>

	  <child>
	    <widget class="GtkButton" id="button1">
	      <property name="width_request">88</property>
	      <property name="visible">True</property>
	      <property name="can_focus">True</property>
	      <property name="label">gtk-execute</property>
	      <property name="use_stock">True</property>
	      <property name="relief">GTK_RELIEF_NORMAL</property>
	    </widget>
	    <packing>
	      <property name="padding">0</property>
	      <property name="expand">True</property>
	      <property name="fill">False</property>
	    </packing>
	  </child>
	</widget>
	<packing>
	  <property name="padding">0</property>
	  <property name="expand">False</property>
	  <property name="fill">False</property>
	</packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>
packages=gtkmm-2.0 libglademm-2.0 glibmm-2.0 gthread-2.0
exemplo03: exemplo03.cc exemplo03.glade
	g++ -o exemplo03 exemplo03.cc `pkg-config --cflags --libs $(packages)`


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