Fawkes API  Fawkes Development Version
qa_rwlock.cpp
1 
2 /***************************************************************************
3  * example_rwlock.cpp - Example for an ReadWriteeLock
4  *
5  * Generated: Fri Sep 15 11:53:23 2006
6  * Copyright 2006 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/read_write_lock.h>
28 
29 #include <iostream>
30 #include <string>
31 
32 using namespace std;
33 using namespace fawkes;
34 
35 /** Example writer thread, will aquire lock and increment
36  * the value. Will print out a notice if it has
37  * to wait for readers.
38  */
39 
40 class ExampleRWLockWriterThread : public Thread
41 {
42  public:
43  ExampleRWLockWriterThread(ReadWriteLock *rwlock, int *val, unsigned int sleep_time)
44  : Thread("ExampleRWLockWriterThread", Thread::OPMODE_CONTINUOUS)
45  {
46  this->rwlock = rwlock;
47  this->val = val;
48  this->sleep_time = sleep_time;
49  }
50 
51  /** Action!
52  */
53  virtual void loop()
54  {
55  if ( ! rwlock->try_lock_for_write() ) {
56  cout << "Writer: Readers on lock, waiting for release" << endl;
57  rwlock->lock_for_write();
58  // aquired the lock
59  }
60  cout << "Writer: aquired lock" << endl;
61  (*val)++;
62  usleep(sleep_time);
63  rwlock->unlock();
64  }
65 
66  private:
67  ReadWriteLock *rwlock;
68  int *val;
69  unsigned int sleep_time;
70 };
71 
72 
73 /** Example reader thread, will aquire reader lock and print out
74  * the current value. Will print a notice if it has to wait for a writer
75  * to unlock the lock.
76  */
77 class ExampleRWLockReaderThread : public Thread
78 {
79  public:
80  ExampleRWLockReaderThread(string pp,
81  ReadWriteLock *rwlock, int *val, unsigned int sleep_time)
82  : Thread("ExampleRWLockReaderThread", Thread::OPMODE_CONTINUOUS)
83  {
84  this->pp = pp;
85  this->rwlock = rwlock;
86  this->val = val;
87  this->sleep_time = sleep_time;
88  }
89 
90  virtual void loop()
91  {
92  if ( ! rwlock->try_lock_for_read() ) {
93  cout << "Reader (" << pp << "): Writer on lock, waiting for release" << endl;
94  rwlock->lock_for_read();
95  }
96  cout << "Reader (" << pp << "): aquired lock" << endl;
97  cout << "Reader (" << pp << "): val=" << *val << endl;
98  usleep(sleep_time);
99  cout << "Reader (" << pp << "): Unlocking" << endl;
100  rwlock->unlock();
101  }
102 
103  private:
104  string pp;
105  ReadWriteLock *rwlock;
106  int *val;
107  unsigned int sleep_time;
108 };
109 
110 
111 int
112 main(int argc, char **argv)
113 {
114  int val = 0;
115 
116  ReadWriteLock *rwlock = new ReadWriteLock();
117 
118  ExampleRWLockWriterThread *tw = new ExampleRWLockWriterThread(rwlock, &val, 100000);
119  ExampleRWLockReaderThread *tr1 = new ExampleRWLockReaderThread("r1", rwlock, &val, 234234);
120  ExampleRWLockReaderThread *tr2 = new ExampleRWLockReaderThread("r2", rwlock, &val, 156743);
121  ExampleRWLockReaderThread *tr3 = new ExampleRWLockReaderThread("r3", rwlock, &val, 623442);
122  ExampleRWLockReaderThread *tr4 = new ExampleRWLockReaderThread("r4", rwlock, &val, 455345);
123 
124  tw->start();
125  tr1->start();
126  tr2->start();
127  tr3->start();
128  tr4->start();
129 
130  tw->join();
131  tr1->join();
132  tr2->join();
133  tr3->join();
134  tr4->join();
135 
136  delete tw;
137  delete tr1;
138  delete tr2;
139  delete tr3;
140  delete tr4;
141  delete rwlock;
142 
143  return 0;
144 }
145 
146 
147 /// @endcond
void lock_for_read()
Aquire a reader lock.
Fawkes library namespace.
STL namespace.
Thread class encapsulation of pthreads.
Definition: thread.h:42
void lock_for_write()
Aquire a writer lock.
Read/write lock to allow multiple readers but only a single writer on the resource at a time...