Fawkes API  Fawkes Development Version
qa_waitcond_serialize.cpp
1 
2 /***************************************************************************
3  * example_waitcond_serialize.cpp - example application for using condition
4  * variables to serialize threads
5  *
6  * Generated: Thu Sep 14 21:43:30 2006
7  * Copyright 2006 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL file in the doc directory.
22  */
23 
24 /// @cond EXAMPLES
25 
26 #include <core/threading/thread.h>
27 #include <core/threading/wait_condition.h>
28 #include <core/threading/mutex.h>
29 
30 #include <iostream>
31 
32 using namespace std;
33 using namespace fawkes;
34 
35 /** Small example hread serializing with other threads using a wait condition.
36  * Run the program and see them printing out numbers serialized.
37  */
38 class ExampleWaitCondThread : public Thread
39 {
40  public:
41  /** Constructor
42  * @param wc Wait condition
43  * @param m Mutex that is locked for the condition variable
44  * @param val Pointer to the current value
45  * @param actval Activation value when this thread becomes active
46  * @param maxval Maximum value when to reset the value
47  */
48  ExampleWaitCondThread(WaitCondition *wc, Mutex *m, int *val, int actval, int maxval)
49  : Thread("ExampleWaitCondThread", Thread::OPMODE_CONTINUOUS)
50  {
51  this->wc = wc;
52  this->m = m;
53  this->val = val;
54  this->actval = actval;
55  this->maxval = maxval;
56  }
57 
58  /** Action!
59  */
60  virtual void loop()
61  {
62  m->lock();
63  while (*val != actval) {
64  wc->wait();
65  }
66  cout << *val << " called" << endl;
67  *val += 1;
68  if ( *val > maxval ) {
69  *val = 0;
70  }
71  // unlock mutex inside wait condition
72  m->unlock();
73 
74  // Cannot call wake_one() here since result is unpredictable and if not
75  // the next thread is woken up we will end up in a deadlock. So every
76  // thread has to check if it's his turn -> use wake_all()
77  wc->wake_all();
78  }
79 
80  private:
81  WaitCondition *wc;
82  Mutex *m;
83 
84  int *val;
85  int actval;
86  int maxval;
87 
88 };
89 
90 /* This small app uses a condition variable to serialize
91  * a couple of threads
92  */
93 int
94 main(int argc, char **argv)
95 {
96 
97  int val = 0;
98 
99  Mutex *m = new Mutex();
100  WaitCondition *wc = new WaitCondition(m);
101 
102  ExampleWaitCondThread *t1 = new ExampleWaitCondThread(wc, m, &val, 0, 4);
103  ExampleWaitCondThread *t2 = new ExampleWaitCondThread(wc, m, &val, 1, 4);
104  ExampleWaitCondThread *t3 = new ExampleWaitCondThread(wc, m, &val, 2, 4);
105  ExampleWaitCondThread *t4 = new ExampleWaitCondThread(wc, m, &val, 3, 4);
106  ExampleWaitCondThread *t5 = new ExampleWaitCondThread(wc, m, &val, 4, 4);
107 
108  t1->start();
109  t2->start();
110  t3->start();
111  t4->start();
112  t5->start();
113 
114  t1->join();
115  t2->join();
116  t3->join();
117  t4->join();
118  t5->join();
119 
120  delete t5;
121  delete t4;
122  delete t3;
123  delete t2;
124  delete t1;
125  delete wc;
126  delete m;
127 
128  return 0;
129 }
130 
131 
132 /// @endcond
Wait until a given condition holds.
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void lock()
Lock this mutex.
Definition: mutex.cpp:89
Mutex mutual exclusion lock.
Definition: mutex.h:32