qa_waitcond_serialize.cpp

00001 
00002 /***************************************************************************
00003  *  example_waitcond_serialize.cpp - example application for using condition
00004  *                                   variables to serialize threads
00005  *
00006  *  Generated: Thu Sep 14 21:43:30 2006
00007  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010 
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL file in the doc directory.
00022  */
00023 
00024 /// @cond EXAMPLES
00025 
00026 #include <core/threading/thread.h>
00027 #include <core/threading/wait_condition.h>
00028 #include <core/threading/mutex.h>
00029 
00030 #include <iostream>
00031 
00032 using namespace std;
00033 using namespace fawkes;
00034 
00035 /** Small example hread serializing with other threads using a wait condition.
00036  * Run the program and see them printing out numbers serialized.
00037  */
00038 class ExampleWaitCondThread : public Thread
00039 {
00040  public:
00041   /** Constructor
00042    * @param wc Wait condition
00043    * @param m Mutex that is locked for the condition variable
00044    * @param val Pointer to the current value
00045    * @param actval Activation value when this thread becomes active
00046    * @param maxval Maximum value when to reset the value
00047    */
00048   ExampleWaitCondThread(WaitCondition *wc, Mutex *m, int *val, int actval, int maxval)
00049     : Thread("ExampleWaitCondThread", Thread::OPMODE_CONTINUOUS)
00050   {
00051     this->wc     = wc;
00052     this->m      = m;
00053     this->val    = val;
00054     this->actval = actval;
00055     this->maxval = maxval;
00056   }
00057 
00058   /** Action!
00059    */
00060   virtual void loop()
00061   {
00062     m->lock();
00063     while (*val != actval) {
00064       wc->wait();
00065     }
00066     cout << *val << " called" << endl;
00067     *val += 1;
00068     if ( *val > maxval ) {
00069       *val = 0;
00070     }
00071     // unlock mutex inside wait condition
00072     m->unlock();
00073     
00074     // Cannot call wake_one() here since result is unpredictable and if not
00075     // the next thread is woken up we will end up in a deadlock. So every
00076     // thread has to check if it's his turn -> use wake_all()
00077     wc->wake_all();
00078   }
00079 
00080  private:
00081   WaitCondition *wc;
00082   Mutex         *m;
00083 
00084   int           *val;
00085   int            actval;
00086   int            maxval;
00087 
00088 };
00089 
00090 /* This small app uses a condition variable to serialize
00091  * a couple of threads
00092  */
00093 int
00094 main(int argc, char **argv)
00095 {
00096 
00097   int val = 0;
00098 
00099   Mutex *m = new Mutex();
00100   WaitCondition *wc = new WaitCondition(m);
00101 
00102   ExampleWaitCondThread *t1 = new ExampleWaitCondThread(wc, m, &val, 0, 4);
00103   ExampleWaitCondThread *t2 = new ExampleWaitCondThread(wc, m, &val, 1, 4);
00104   ExampleWaitCondThread *t3 = new ExampleWaitCondThread(wc, m, &val, 2, 4);
00105   ExampleWaitCondThread *t4 = new ExampleWaitCondThread(wc, m, &val, 3, 4);
00106   ExampleWaitCondThread *t5 = new ExampleWaitCondThread(wc, m, &val, 4, 4);
00107 
00108   t1->start();
00109   t2->start();
00110   t3->start();
00111   t4->start();
00112   t5->start();
00113 
00114   t1->join();
00115   t2->join();
00116   t3->join();
00117   t4->join();
00118   t5->join();
00119 
00120   delete t5;
00121   delete t4;
00122   delete t3;
00123   delete t2;
00124   delete t1;
00125   delete wc;
00126   delete m;
00127 
00128   return 0;
00129 }
00130 
00131 
00132 /// @endcond

Generated on Tue Feb 22 13:31:25 2011 for Fawkes API by  doxygen 1.4.7