Will this code allow for write preference? [closed]
up vote
0
down vote
favorite
I'm still learning about concurrency, so I'm trying to figure out if this code, which I've modified, gives writers preference. I've been reading some possible solutions and apparently by having a reader locking the write lock then immediately release it we can provide write preference. Why is this?
Also, for clarity, LR
and LW
are class attributes in RW
class.
import threading
class Reader(threading.Thread):
def run(self):
while True:
with RW.LW:
pass
with RW.LR:
RW.read_count += 1
readData(RW.data)
with RW.LR:
RW.read_count -= 1
RW.LR.notify()
doMoreWork()
class Writer(threading.Thread):
def run(self):
while True:
with RW.LW:
waiting = True
while waiting:
with RW.LR:
if RW.read_count == 0:
updateData(Rw.data)
waiting = False
else:
# use wait/notify to avoid busy waiting
while RW.read_count != 0:
RW.LR.wait()
doOtherWork()
python-2.x concurrency
New contributor
closed as off-topic by Edward, Quill, Jamal♦ Nov 14 at 4:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Edward, Quill, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
I'm still learning about concurrency, so I'm trying to figure out if this code, which I've modified, gives writers preference. I've been reading some possible solutions and apparently by having a reader locking the write lock then immediately release it we can provide write preference. Why is this?
Also, for clarity, LR
and LW
are class attributes in RW
class.
import threading
class Reader(threading.Thread):
def run(self):
while True:
with RW.LW:
pass
with RW.LR:
RW.read_count += 1
readData(RW.data)
with RW.LR:
RW.read_count -= 1
RW.LR.notify()
doMoreWork()
class Writer(threading.Thread):
def run(self):
while True:
with RW.LW:
waiting = True
while waiting:
with RW.LR:
if RW.read_count == 0:
updateData(Rw.data)
waiting = False
else:
# use wait/notify to avoid busy waiting
while RW.read_count != 0:
RW.LR.wait()
doOtherWork()
python-2.x concurrency
New contributor
closed as off-topic by Edward, Quill, Jamal♦ Nov 14 at 4:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Edward, Quill, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm still learning about concurrency, so I'm trying to figure out if this code, which I've modified, gives writers preference. I've been reading some possible solutions and apparently by having a reader locking the write lock then immediately release it we can provide write preference. Why is this?
Also, for clarity, LR
and LW
are class attributes in RW
class.
import threading
class Reader(threading.Thread):
def run(self):
while True:
with RW.LW:
pass
with RW.LR:
RW.read_count += 1
readData(RW.data)
with RW.LR:
RW.read_count -= 1
RW.LR.notify()
doMoreWork()
class Writer(threading.Thread):
def run(self):
while True:
with RW.LW:
waiting = True
while waiting:
with RW.LR:
if RW.read_count == 0:
updateData(Rw.data)
waiting = False
else:
# use wait/notify to avoid busy waiting
while RW.read_count != 0:
RW.LR.wait()
doOtherWork()
python-2.x concurrency
New contributor
I'm still learning about concurrency, so I'm trying to figure out if this code, which I've modified, gives writers preference. I've been reading some possible solutions and apparently by having a reader locking the write lock then immediately release it we can provide write preference. Why is this?
Also, for clarity, LR
and LW
are class attributes in RW
class.
import threading
class Reader(threading.Thread):
def run(self):
while True:
with RW.LW:
pass
with RW.LR:
RW.read_count += 1
readData(RW.data)
with RW.LR:
RW.read_count -= 1
RW.LR.notify()
doMoreWork()
class Writer(threading.Thread):
def run(self):
while True:
with RW.LW:
waiting = True
while waiting:
with RW.LR:
if RW.read_count == 0:
updateData(Rw.data)
waiting = False
else:
# use wait/notify to avoid busy waiting
while RW.read_count != 0:
RW.LR.wait()
doOtherWork()
python-2.x concurrency
python-2.x concurrency
New contributor
New contributor
New contributor
asked Nov 13 at 21:58
tofu_bacon
1011
1011
New contributor
New contributor
closed as off-topic by Edward, Quill, Jamal♦ Nov 14 at 4:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Edward, Quill, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Edward, Quill, Jamal♦ Nov 14 at 4:16
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – Edward, Quill, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes