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/mutex.h>
27 #include <core/threading/thread.h>
28 #include <core/threading/wait_condition.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
61  loop()
62  {
63  m->lock();
64  while (*val != actval) {
65  wc->wait();
66  }
67  cout << *val << " called" << endl;
68  *val += 1;
69  if (*val > maxval) {
70  *val = 0;
71  }
72  // unlock mutex inside wait condition
73  m->unlock();
74 
75  // Cannot call wake_one() here since result is unpredictable and if not
76  // the next thread is woken up we will end up in a deadlock. So every
77  // thread has to check if it's his turn -> use wake_all()
78  wc->wake_all();
79  }
80 
81 private:
82  WaitCondition *wc;
83  Mutex * m;
84 
85  int *val;
86  int actval;
87  int maxval;
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  int val = 0;
97 
98  Mutex * m = new Mutex();
99  WaitCondition *wc = new WaitCondition(m);
100 
101  ExampleWaitCondThread *t1 = new ExampleWaitCondThread(wc, m, &val, 0, 4);
102  ExampleWaitCondThread *t2 = new ExampleWaitCondThread(wc, m, &val, 1, 4);
103  ExampleWaitCondThread *t3 = new ExampleWaitCondThread(wc, m, &val, 2, 4);
104  ExampleWaitCondThread *t4 = new ExampleWaitCondThread(wc, m, &val, 3, 4);
105  ExampleWaitCondThread *t5 = new ExampleWaitCondThread(wc, m, &val, 4, 4);
106 
107  t1->start();
108  t2->start();
109  t3->start();
110  t4->start();
111  t5->start();
112 
113  t1->join();
114  t2->join();
115  t3->join();
116  t4->join();
117  t5->join();
118 
119  delete t5;
120  delete t4;
121  delete t3;
122  delete t2;
123  delete t1;
124  delete wc;
125  delete m;
126 
127  return 0;
128 }
129 
130 /// @endcond
fawkes::Mutex::lock
void lock()
Lock this mutex.
Definition: mutex.cpp:93
fawkes::Mutex
Definition: mutex.h:38
fawkes::WaitCondition
Definition: wait_condition.h:42
fawkes
fawkes::Thread
Definition: thread.h:45