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/read_write_lock.h>
27#include <core/threading/thread.h>
28
29#include <iostream>
30#include <string>
31
32using namespace std;
33using 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
40class ExampleRWLockWriterThread : public Thread
41{
42public:
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
54 loop()
55 {
56 if (!rwlock->try_lock_for_write()) {
57 cout << "Writer: Readers on lock, waiting for release" << endl;
58 rwlock->lock_for_write();
59 // aquired the lock
60 }
61 cout << "Writer: aquired lock" << endl;
62 (*val)++;
63 usleep(sleep_time);
64 rwlock->unlock();
65 }
66
67private:
68 ReadWriteLock *rwlock;
69 int * val;
70 unsigned int sleep_time;
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 */
77class ExampleRWLockReaderThread : public Thread
78{
79public:
80 ExampleRWLockReaderThread(string pp, ReadWriteLock *rwlock, int *val, unsigned int sleep_time)
81 : Thread("ExampleRWLockReaderThread", Thread::OPMODE_CONTINUOUS)
82 {
83 this->pp = pp;
84 this->rwlock = rwlock;
85 this->val = val;
86 this->sleep_time = sleep_time;
87 }
88
89 virtual void
90 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
103private:
104 string pp;
105 ReadWriteLock *rwlock;
106 int * val;
107 unsigned int sleep_time;
108};
109
110int
111main(int argc, char **argv)
112{
113 int val = 0;
114
115 ReadWriteLock *rwlock = new ReadWriteLock();
116
117 ExampleRWLockWriterThread *tw = new ExampleRWLockWriterThread(rwlock, &val, 100000);
118 ExampleRWLockReaderThread *tr1 = new ExampleRWLockReaderThread("r1", rwlock, &val, 234234);
119 ExampleRWLockReaderThread *tr2 = new ExampleRWLockReaderThread("r2", rwlock, &val, 156743);
120 ExampleRWLockReaderThread *tr3 = new ExampleRWLockReaderThread("r3", rwlock, &val, 623442);
121 ExampleRWLockReaderThread *tr4 = new ExampleRWLockReaderThread("r4", rwlock, &val, 455345);
122
123 tw->start();
124 tr1->start();
125 tr2->start();
126 tr3->start();
127 tr4->start();
128
129 tw->join();
130 tr1->join();
131 tr2->join();
132 tr3->join();
133 tr4->join();
134
135 delete tw;
136 delete tr1;
137 delete tr2;
138 delete tr3;
139 delete tr4;
140 delete rwlock;
141
142 return 0;
143}
144
145/// @endcond
Read/write lock to allow multiple readers but only a single writer on the resource at a time.
void lock_for_read()
Aquire a reader lock.
void lock_for_write()
Aquire a writer lock.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.