How to program critical section for reader-writer systems?
Posted
by Srinivas Nayak
on Stack Overflow
See other posts from Stack Overflow
or by Srinivas Nayak
Published on 2010-05-20T06:46:27Z
Indexed on
2010/05/20
6:50 UTC
Read the original article
Hit count: 336
readerwriterlock
Hi,
Lets say, I have a reader-writer system where reader and writer are concurrently running. 'a' and 'b' are two shared variables, which are related to each other, so modification to them needs to be an atomic operation.
A reader-writer system can be of the following types:
- rr
- ww
- r-w
- r-ww
- rr-w
- rr-ww
where
[ r : single reader
rr: multiple reader
w : single writer
ww: multiple writer ]
Now, We can have a read method for a reader and a write method for a writer as follows. I have written them system type wise.
rr
read_method { read a; read b; }
ww
write_method { lock(m); write a; write b; unlock(m); }
r-w
- r-ww
- rr-w
rr-ww
read_method { lock(m); read a; read b; unlock(m); } write_method { lock(m); write a; write b; unlock(m); }
For multiple reader system, shared variable access doesn't need to be atomic.
For multiple writer system, shared variable access need to be atomic, so locked with 'm'.
But, for system types 3 to 6, is my read_method and write_method correct? How can I improve?
Sincerely,
Srinivas Nayak
© Stack Overflow or respective owner