Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * example_rwlock.cpp - Example for an ReadWriteeLock 00004 * 00005 * Generated: Fri Sep 15 11:53:23 2006 00006 * Copyright 2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 // Do not mention in API doc 00024 /// @cond EXAMPLES 00025 00026 #include <core/threading/thread.h> 00027 #include <core/threading/read_write_lock.h> 00028 00029 #include <iostream> 00030 #include <string> 00031 00032 using namespace std; 00033 using namespace fawkes; 00034 00035 /** Example writer thread, will aquire lock and increment 00036 * the value. Will print out a notice if it has 00037 * to wait for readers. 00038 */ 00039 00040 class ExampleRWLockWriterThread : public Thread 00041 { 00042 public: 00043 ExampleRWLockWriterThread(ReadWriteLock *rwlock, int *val, unsigned int sleep_time) 00044 : Thread("ExampleRWLockWriterThread", Thread::OPMODE_CONTINUOUS) 00045 { 00046 this->rwlock = rwlock; 00047 this->val = val; 00048 this->sleep_time = sleep_time; 00049 } 00050 00051 /** Action! 00052 */ 00053 virtual void loop() 00054 { 00055 if ( ! rwlock->try_lock_for_write() ) { 00056 cout << "Writer: Readers on lock, waiting for release" << endl; 00057 rwlock->lock_for_write(); 00058 // aquired the lock 00059 } 00060 cout << "Writer: aquired lock" << endl; 00061 (*val)++; 00062 usleep(sleep_time); 00063 rwlock->unlock(); 00064 } 00065 00066 private: 00067 ReadWriteLock *rwlock; 00068 int *val; 00069 unsigned int sleep_time; 00070 }; 00071 00072 00073 /** Example reader thread, will aquire reader lock and print out 00074 * the current value. Will print a notice if it has to wait for a writer 00075 * to unlock the lock. 00076 */ 00077 class ExampleRWLockReaderThread : public Thread 00078 { 00079 public: 00080 ExampleRWLockReaderThread(string pp, 00081 ReadWriteLock *rwlock, int *val, unsigned int sleep_time) 00082 : Thread("ExampleRWLockReaderThread", Thread::OPMODE_CONTINUOUS) 00083 { 00084 this->pp = pp; 00085 this->rwlock = rwlock; 00086 this->val = val; 00087 this->sleep_time = sleep_time; 00088 } 00089 00090 virtual void loop() 00091 { 00092 if ( ! rwlock->try_lock_for_read() ) { 00093 cout << "Reader (" << pp << "): Writer on lock, waiting for release" << endl; 00094 rwlock->lock_for_read(); 00095 } 00096 cout << "Reader (" << pp << "): aquired lock" << endl; 00097 cout << "Reader (" << pp << "): val=" << *val << endl; 00098 usleep(sleep_time); 00099 cout << "Reader (" << pp << "): Unlocking" << endl; 00100 rwlock->unlock(); 00101 } 00102 00103 private: 00104 string pp; 00105 ReadWriteLock *rwlock; 00106 int *val; 00107 unsigned int sleep_time; 00108 }; 00109 00110 00111 int 00112 main(int argc, char **argv) 00113 { 00114 int val = 0; 00115 00116 ReadWriteLock *rwlock = new ReadWriteLock(); 00117 00118 ExampleRWLockWriterThread *tw = new ExampleRWLockWriterThread(rwlock, &val, 100000); 00119 ExampleRWLockReaderThread *tr1 = new ExampleRWLockReaderThread("r1", rwlock, &val, 234234); 00120 ExampleRWLockReaderThread *tr2 = new ExampleRWLockReaderThread("r2", rwlock, &val, 156743); 00121 ExampleRWLockReaderThread *tr3 = new ExampleRWLockReaderThread("r3", rwlock, &val, 623442); 00122 ExampleRWLockReaderThread *tr4 = new ExampleRWLockReaderThread("r4", rwlock, &val, 455345); 00123 00124 tw->start(); 00125 tr1->start(); 00126 tr2->start(); 00127 tr3->start(); 00128 tr4->start(); 00129 00130 tw->join(); 00131 tr1->join(); 00132 tr2->join(); 00133 tr3->join(); 00134 tr4->join(); 00135 00136 delete tw; 00137 delete tr1; 00138 delete tr2; 00139 delete tr3; 00140 delete tr4; 00141 delete rwlock; 00142 00143 return 0; 00144 } 00145 00146 00147 /// @endcond