Fawkes API  Fawkes Development Version
qa_waitcond.cpp
1 
2 /***************************************************************************
3  * example_waitcond.cpp - wait condition example program
4  *
5  * Created: Sat Mar 01 15:13:44 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 // Do not mention in API doc
24 /// @cond EXAMPLES
25 
26 #include <core/threading/thread.h>
27 #include <core/threading/wait_condition.h>
28 #include <core/exception.h>
29 #include <core/threading/mutex.h>
30 
31 #include <iostream>
32 #include <string>
33 
34 using namespace std;
35 using namespace fawkes;
36 
37 typedef enum {
38  WAITER,
39  WAKER
40 } threadmode_t;
41 
42 class ExampleWaitCondThread : public Thread
43 {
44  public:
45  ExampleWaitCondThread(threadmode_t mode, string tname,
46  WaitCondition *waitcond, unsigned int sleep_time)
47  : Thread(tname.c_str(), Thread::OPMODE_CONTINUOUS)
48  {
49  __mode = mode;
50  __waitcond = waitcond;
51  __sleep_time = sleep_time;
52  }
53 
54  virtual void loop()
55  {
56  if ( __mode == WAITER ) {
57  usleep( __sleep_time );
58  cout << name() << ": Waiting for waker" << endl;
59  try {
60  __waitcond->wait();
61  cout << name() << ": Woken up" << endl;
62  } catch (Exception &e) {
63  cout << name() << ": EXCEPTION" << endl;
64  e.print_trace();
65  }
66  } else { // WAKER
67  usleep( __sleep_time );
68  cout << name() << ": Waking waiter" << endl;
69  __waitcond->wake_all();
70  cout << name() << ": Woke waiter" << endl;
71  }
72  }
73 
74  private:
75  threadmode_t __mode;
76  WaitCondition *__waitcond;
77  unsigned int __sleep_time;
78 
79 };
80 
81 
82 int
83 main(int argc, char **argv)
84 {
85  WaitCondition *w = new WaitCondition();
86 
87  ExampleWaitCondThread *t1 = new ExampleWaitCondThread(WAITER, "waiter1", w, 0);
88  ExampleWaitCondThread *t2 = new ExampleWaitCondThread(WAITER, "waiter2", w, 0);
89  ExampleWaitCondThread *tw = new ExampleWaitCondThread(WAKER, "waker", w, 2458642);
90 
91  t1->start();
92  t2->start();
93  tw->start();
94 
95  t1->join();
96  t2->join();
97  tw->join();
98 
99  delete t1;
100  delete t2;
101  delete tw;
102 
103  delete w;
104 }
105 
106 
107 /// @endcond
Wait until a given condition holds.
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619