multithreading - Threads in Python 2.7 -
i'm new python. tried code readers-writers problem stron reader priority, reason, python doesn't consider global variable 'nor'. there issue thread definition?
import threading readers=threading.boundedsemaphore(1) writers=threading.boundedsemaphore(1) mutex=threading.boundedsemaphore(1) nor=0 class reader(threading.thread): def __init__(self): threading.thread.__init__(self) def run(self): while(1): writers.acquire() mutex.acquire() if(nor==0): readers.acquire() nor=nor+1 mutex.release() print "i read\n" mutex.acquire() if(nor==1): readers.release() nor=nor-1 mutex.release() writers.release() class writer(threading.thread): def _init__(self): threading.thread.__init__(self) def run(self): while(1): writers.acquire() readers.acquire() print "i wrote\n" writers.release() readers.release() r1=reader() r2=reader() r3=reader() w1=writer() w2=writer() r1.start() r2.start() r3.start() w1.start() w2.start()
try tiping
global nor
inside class, before nor call. after
def run(self):
Comments
Post a Comment