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/mutex.h>
28#include <core/threading/thread.h>
29#include <core/threading/wait_condition.h>
30
31#include <iostream>
32#include <string>
33#include <vector>
34
35using namespace std;
36using 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 */
45class ExampleMutexWaitThread : public Thread
46{
47public:
48 ExampleMutexWaitThread(string s) : Thread("ExampleMutexWaitThread", Thread::OPMODE_CONTINUOUS)
49 {
50 this->s = s;
51
52 m.lock();
53 }
54
55 ~ExampleMutexWaitThread()
56 {
57 }
58
59 void
60 wake()
61 {
62 m.unlock();
63 ;
64 }
65
66 string
67 getS()
68 {
69 return s;
70 }
71
72 /** Action!
73 */
74 virtual void
75 loop()
76 {
77 m.lock();
78 cout << s << ": my turn" << endl;
79 // unlock mutex inside wait condition
80 m.unlock();
81 }
82
83private:
84 Mutex m;
85 string s;
86};
87
88class ExampleMutexWaitStarterThread : public Thread
89{
90public:
91 ExampleMutexWaitStarterThread()
92 : Thread("ExampleMutexWaitStarterThread", Thread::OPMODE_CONTINUOUS)
93 {
94 threads.clear();
95 }
96
97 void
98 wakeThreads()
99 {
100 vector<ExampleMutexWaitThread *>::iterator tit;
101 for (tit = threads.begin(); tit != threads.end(); ++tit) {
102 cout << "Waking thread " << (*tit)->getS() << endl;
103 (*tit)->wake();
104 }
105 }
106
107 void
108 addThread(ExampleMutexWaitThread *t)
109 {
110 threads.push_back(t);
111 }
112
113 virtual void
114 loop()
115 {
116 sleep(2423423);
117 wakeThreads();
118 }
119
120private:
121 vector<ExampleMutexWaitThread *> threads;
122};
123
124/* This small app uses a condition variable to serialize
125 * a couple of threads
126 */
127int
128main(int argc, char **argv)
129{
130 ExampleMutexWaitThread *t1 = new ExampleMutexWaitThread("t1");
131 ExampleMutexWaitThread *t2 = new ExampleMutexWaitThread("t2");
132 ExampleMutexWaitThread *t3 = new ExampleMutexWaitThread("t3");
133
134 ExampleMutexWaitStarterThread *st = new ExampleMutexWaitStarterThread();
135 st->addThread(t1);
136 st->addThread(t2);
137 st->addThread(t3);
138
139 t1->start();
140 t2->start();
141 t3->start();
142 st->start();
143
144 t1->join();
145 t2->join();
146 t3->join();
147 st->join();
148
149 delete st;
150 delete t3;
151 delete t2;
152 delete t1;
153
154 return 0;
155}
156
157/// @endcond
Mutex mutual exclusion lock.
Definition: mutex.h:33
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.