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()









share|improve this question







New contributor




tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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.

















    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()









    share|improve this question







    New contributor




    tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    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.















      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()









      share|improve this question







      New contributor




      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question







      New contributor




      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 13 at 21:58









      tofu_bacon

      1011




      1011




      New contributor




      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      tofu_bacon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      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.



























          active

          oldest

          votes






















          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Сан-Квентин

          Алькесар

          Josef Freinademetz