Fawkes API  Fawkes Development Version
qa_barrier.cpp
00001 
00002 /***************************************************************************
00003  *  example_barrier.cpp - barrier example program
00004  *
00005  *  Created: Thu Sep 15 14:48:11 2006
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 // Do not mention in API doc
00024 /// @cond EXAMPLES
00025 
00026 #include <core/threading/thread.h>
00027 #include <core/threading/barrier.h>
00028 
00029 #include <iostream>
00030 #include <string>
00031 
00032 using namespace std;
00033 using namespace fawkes;
00034 
00035 class ExampleBarrierThread : public Thread
00036 {
00037  public:
00038   ExampleBarrierThread(string pp,
00039                        Barrier *barrier, unsigned int sleep_time)
00040     : Thread("ExampleBarrierThread", Thread::OPMODE_CONTINUOUS)
00041   {
00042     this->pp         = pp;
00043     this->barrier    = barrier;
00044     this->sleep_time = sleep_time;
00045   }
00046 
00047   virtual void loop()
00048   {
00049     usleep( sleep_time );
00050     cout << pp << ": Waiting for barrier" << endl;
00051     barrier->wait();
00052     cout << pp << ": Barrier lifted" << endl;
00053   }
00054 
00055  private:
00056   Barrier *barrier;
00057   unsigned int sleep_time;
00058   string pp;
00059 
00060 };
00061 
00062 
00063 int
00064 main(int argc, char **argv)
00065 {
00066   Barrier *b = new Barrier(3);
00067 
00068   ExampleBarrierThread *t1 = new ExampleBarrierThread("t1", b, 3424345);
00069   ExampleBarrierThread *t2 = new ExampleBarrierThread("t2", b, 326545);
00070   ExampleBarrierThread *t3 = new ExampleBarrierThread("t3", b, 6458642);
00071 
00072   t1->start();
00073   t2->start();
00074   t3->start();
00075 
00076   t1->join();
00077   t2->join();
00078   t3->join();
00079 
00080   delete t1;
00081   delete t2;
00082   delete t3;
00083 
00084   delete b;
00085 }
00086 
00087 
00088 /// @endcond