examples/parallel-threads/parallel-threads.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <iostream>
00040 
00041 #include <QVApplication>
00042 #include <QVDefaultGUI>
00043 
00044 #ifndef DOXYGEN_IGNORE_THIS
00045 class MyWorker1: public QVWorker
00046         {
00047         public:
00048                 MyWorker1(QString name):QVWorker(name)
00049                         {
00050                         addProperty<int>("int value", inputFlag|outputFlag, 10, "Integer test property", 10, 100);
00051                         addProperty<double>("double value", inputFlag|outputFlag, 1.5, "Double test property", 0.0, 10.0);
00052                         addProperty<bool>("boolean value", inputFlag|outputFlag, TRUE, "Boolean test property");
00053                         }
00054 
00055                 void iterate()
00056                         {
00057                         int i,j=0;
00058                         for(i=0;i<50000*2;i++) j=j+qrand();
00059                         timeFlag("Tiempo 1");
00060                         for(i=0;i<100000*2;i++) j=j+qrand();
00061                         timeFlag("Tiempo 2");
00062                         for(i=0;i<150000*2;i++) j=j+qrand();
00063                         timeFlag("Tiempo 3");
00064                         
00065                         // Access to properties:
00066                         int v1 = getPropertyValue<int>("int value");
00067                         double v2 = getPropertyValue<double>("double value");
00068                         bool v3 = getPropertyValue<bool>("boolean value");
00069                         std::cout << "\tint value= " << v1 << "\tdouble value = " << v2 << "\tboolean value = " << v3 << "\n";
00070                         }
00071         };
00072 
00073 class MyWorker2: public QVWorker
00074         {
00075         private:
00076                 int id;
00077 
00078         public:
00079                 MyWorker2(QString name,int _id):QVWorker(name), id(_id)
00080                         {
00081                         addProperty<int>("int value", inputFlag|outputFlag, 60, "Integer test property", 50, 70);
00082                         addProperty<int>(QString("i%1").arg(id), outputFlag|inputFlag,15, "Integer test property", 10, 100);
00083                         addProperty<double>(QString("d%1").arg(id), outputFlag|inputFlag, 1.5, "Double test property", 0.0, 10.0);
00084                         addProperty<bool>(QString("b%1").arg(id), outputFlag|inputFlag, TRUE, "Boolean test property");
00085                         }
00086 
00087                 void iterate()
00088                         {
00089                         int i,j=0;
00090                         for(i=0;i<50000*2*id;i++) j=j+qrand();
00091                         timeFlag("Tiempo 1");
00092 
00093                         // Access to properties:
00094                         int vi = getPropertyValue<int>(QString("i%1").arg(id));
00095                         double vd = getPropertyValue<double>(QString("d%1").arg(id));
00096                         bool vb = getPropertyValue<bool>(QString("b%1").arg(id));
00097 
00098                         QString msg;
00099                         for(int i=0;i<id;i++) 
00100                                 msg += "\t\t";
00101 
00102                         msg +=  QString("i%1=%2").arg(id).arg(vi) + QString("\td%1=%2").arg(id).arg(vd) + QString("\tb%1=%2 ").arg(id).arg(vb) + "\n";
00103                         std::cout << qPrintable(msg);
00104                         }
00105         };
00106 
00107 int main(int argc, char *argv[])
00108         {
00109         QVApplication app(argc, argv,
00110                 "Example program for QVision library. Shows the use of QVApllication and QVWorker classes."
00111                 );
00112 
00113         MyWorker1 worker1("W1");
00114         MyWorker2 worker2("W2",2), worker3("W3",3), worker4("W4",4);
00115 
00116         worker1.linkProperty("double value", &worker2, "d2",    QVWorker::SynchronousLink);
00117         worker1.linkProperty("int value", &worker3, "i3",       QVWorker::SynchronousLink);
00118         worker1.linkProperty("double value", &worker3, "d3",    QVWorker::SynchronousLink);
00119         worker1.linkProperty("boolean value", &worker3, "b3",   QVWorker::SynchronousLink);
00120         worker1.linkProperty("boolean value", &worker4, "b4",   QVWorker::AsynchronousLink);
00121 
00122         QVDefaultGUI interface;
00123 
00124         return app.exec();
00125         }
00126 
00127 #endif
00128