Fawkes API  Fawkes Development Version
qa_mutex_count.cpp
1 
2 /***************************************************************************
3  * example_mutx_count.cpp - Example for counting with multiple threads and
4  * protecting the count variable with a mutex
5  *
6  * Generated: Thu Sep 14 16:29:37 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 #include <core/threading/thread.h>
25 #include <core/threading/mutex.h>
26 
27 #include <iostream>
28 
29 // By default do not include examples in API documentation
30 /// @cond EXAMPLES
31 
32 using namespace std;
33 using namespace fawkes;
34 
35 #define WASTETIME \
36  for ( unsigned int i = 0; i < 1000000; i++) { \
37  unsigned int j; \
38  j = i + i; \
39  }
40 
41 
42 /** Simple test class for counting with multiple threads.
43  * Compile the test program and let it run. You will see that even after only a short time
44  * the values for the protected and the unprotected count variables differ.
45  */
46 class ExampleMutexCountThread : public Thread
47 {
48 public:
49  /** Constructor
50  * @param s Short identifier, printed first in output
51  * @param m The mutex used to protect count variable
52  * @param mutex_count Protected count variable
53  * @param non_mutex_count Unprotected count variable
54  * @param sleep_time Variable sleep time at end of thread
55  */
56  ExampleMutexCountThread(string s,
57  Mutex *m, unsigned int *mutex_count, unsigned int *non_mutex_count,
58  unsigned int sleep_time)
59  : Thread("ExampMutexCountThread", Thread::OPMODE_CONTINUOUS)
60  {
61  this->s = s;
62  this->sl = sl;
63  this->slt = sleep_time;
64  this->m = m;
65  this->mc = mutex_count;
66  this->nmc = non_mutex_count;
67  }
68 
69  /** Where the action happens
70  */
71  virtual void loop()
72  {
73  // unprotected modification, another thread could modify the value while
74  // we waste time
75  unsigned int n = *nmc;
76  n++;
77  sleep(0);
78  WASTETIME;
79  *nmc = n;
80 
81  // protected modification, no other thread can modify the value as long as
82  // we have the lock
83  if ( m != NULL ) m->lock();
84  unsigned o = *mc;
85  o++;
86  sleep(0);
87  WASTETIME;
88  *mc = o;
89  if ( m != NULL ) m->unlock();
90 
91  // Out is not mutexed, can lead to wrong printouts, try it (happens rarely)!
92  cout << s << ": mutex: " << *mc << "(non-mutex: " << *nmc << ")" << endl;
93 
94  if ( sl ) usleep(slt);
95 
96  test_cancel();
97  }
98 
99  private:
100  string s;
101  bool sl;
102  unsigned int slt;
103  Mutex *m;
104  unsigned int *mc;
105  unsigned int *nmc;
106 };
107 
108 
109 int
110 main(int argc, char **argv)
111 {
112 
113  Mutex *m = new Mutex();
114 
115  unsigned int mutex_count = 0;
116  unsigned int non_mutex_count = 0;
117 
118  ExampleMutexCountThread *t1 = new ExampleMutexCountThread("t1", m, &mutex_count, &non_mutex_count, 1000);
119  ExampleMutexCountThread *t2 = new ExampleMutexCountThread("t2", m, &mutex_count, &non_mutex_count, 10000);
120  ExampleMutexCountThread *t3 = new ExampleMutexCountThread("t3", m, &mutex_count, &non_mutex_count, 100000);
121 
122  t1->start();
123  t2->start();
124  t3->start();
125 
126  // Wait for all threads to finish
127  t1->join();
128  t2->join();
129  t3->join();
130 
131  delete t1;
132  delete t2;
133  delete t3;
134  delete m;
135 }
136 
137 
138 /// @endcond
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
Mutex mutual exclusion lock.
Definition: mutex.h:32