26 #include <core/threading/thread.h> 27 #include <core/threading/read_write_lock.h> 40 class ExampleRWLockWriterThread :
public Thread 43 ExampleRWLockWriterThread(
ReadWriteLock *rwlock,
int *val,
unsigned int sleep_time)
44 :
Thread(
"ExampleRWLockWriterThread",
Thread::OPMODE_CONTINUOUS)
46 this->rwlock = rwlock;
48 this->sleep_time = sleep_time;
55 if ( ! rwlock->try_lock_for_write() ) {
56 cout <<
"Writer: Readers on lock, waiting for release" << endl;
60 cout <<
"Writer: aquired lock" << endl;
69 unsigned int sleep_time;
77 class ExampleRWLockReaderThread :
public Thread 80 ExampleRWLockReaderThread(
string pp,
82 :
Thread(
"ExampleRWLockReaderThread",
Thread::OPMODE_CONTINUOUS)
85 this->rwlock = rwlock;
87 this->sleep_time = sleep_time;
92 if ( ! rwlock->try_lock_for_read() ) {
93 cout <<
"Reader (" << pp <<
"): Writer on lock, waiting for release" << endl;
96 cout <<
"Reader (" << pp <<
"): aquired lock" << endl;
97 cout <<
"Reader (" << pp <<
"): val=" << *val << endl;
99 cout <<
"Reader (" << pp <<
"): Unlocking" << endl;
107 unsigned int sleep_time;
112 main(
int argc,
char **argv)
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);
void lock_for_read()
Aquire a reader lock.
Fawkes library namespace.
Thread class encapsulation of pthreads.
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...