Fawkes API  Fawkes Development Version
qa_mutex_sync.cpp
1 
2 /***************************************************************************
3  * example_mutex_sync.cpp - example application for using mutexes
4  * to synchronize several threads to a given point
5  * in time
6  *
7  * Generated: Thu Sep 21 11:55:59 2006
8  * Copyright 2006 Tim Niemueller [www.niemueller.de]
9  *
10  ****************************************************************************/
11 
12 /* This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL file in the doc directory.
23  */
24 
25 /// @cond EXAMPLES
26 
27 #include <core/threading/thread.h>
28 #include <core/threading/wait_condition.h>
29 #include <core/threading/mutex.h>
30 
31 #include <iostream>
32 #include <string>
33 #include <vector>
34 
35 using namespace std;
36 using namespace fawkes;
37 
38 /** Small example hread serializing with other threads using a wait condition.
39  * Run the program and see them printing out numbers serialized.
40  *
41  * NOTE: This can be done more easily by using ThreadList and Threads in
42  * wait-for-wakeup mode! This is just a demonstration to improve understanding
43  * of sync constructs.
44  */
45 class ExampleMutexWaitThread : public Thread
46 {
47  public:
48  ExampleMutexWaitThread(string s)
49  : Thread("ExampleMutexWaitThread", Thread::OPMODE_CONTINUOUS)
50  {
51  this->s = s;
52 
53  m.lock();
54  }
55 
56  ~ExampleMutexWaitThread()
57  {
58  }
59 
60  void wake()
61  {
62  m.unlock();;
63  }
64 
65  string getS()
66  {
67  return s;
68  }
69 
70  /** Action!
71  */
72  virtual void loop()
73  {
74  m.lock();
75  cout << s << ": my turn" << endl;
76  // unlock mutex inside wait condition
77  m.unlock();
78  }
79 
80  private:
81  Mutex m;
82  string s;
83 
84 };
85 
86 class ExampleMutexWaitStarterThread : public Thread
87 {
88  public:
89  ExampleMutexWaitStarterThread()
90  : Thread("ExampleMutexWaitStarterThread", Thread::OPMODE_CONTINUOUS)
91  {
92  threads.clear();
93  }
94 
95  void wakeThreads()
96  {
97  vector< ExampleMutexWaitThread * >::iterator tit;
98  for (tit = threads.begin(); tit != threads.end(); ++tit) {
99  cout << "Waking thread " << (*tit)->getS() << endl;
100  (*tit)->wake();
101  }
102  }
103 
104  void addThread(ExampleMutexWaitThread *t)
105  {
106  threads.push_back(t);
107  }
108 
109 
110  virtual void loop()
111  {
112  sleep(2423423);
113  wakeThreads();
114  }
115 
116  private:
117  vector< ExampleMutexWaitThread * > threads;
118 };
119 
120 /* This small app uses a condition variable to serialize
121  * a couple of threads
122  */
123 int
124 main(int argc, char **argv)
125 {
126 
127  ExampleMutexWaitThread *t1 = new ExampleMutexWaitThread("t1");
128  ExampleMutexWaitThread *t2 = new ExampleMutexWaitThread("t2");
129  ExampleMutexWaitThread *t3 = new ExampleMutexWaitThread("t3");
130 
131  ExampleMutexWaitStarterThread *st = new ExampleMutexWaitStarterThread();
132  st->addThread( t1 );
133  st->addThread( t2 );
134  st->addThread( t3 );
135 
136  t1->start();
137  t2->start();
138  t3->start();
139  st->start();
140 
141  t1->join();
142  t2->join();
143  t3->join();
144  st->join();
145 
146  delete st;
147  delete t3;
148  delete t2;
149  delete t1;
150 
151  return 0;
152 }
153 
154 
155 /// @endcond
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Mutex mutual exclusion lock.
Definition: mutex.h:32