我正在尝试编写一个程序,我希望使用它在两个线程 thread1 和 thread2 之间交替。棘手的部分是我要确保应该开始执行的第一个线程是 thread1。这是我到目前为止的代码,但它不断向我抛出运行时异常。
lock1.release() 错误:释放未锁定的锁
但是,在我看来,lock1 不是正在释放的未锁定锁!
这是我到目前为止的代码
class Client:
#member variables
def sendFile(self,lock1,lock2):
sent = 0
while (i<self.size):
if(sent!=0):
lock2.acquire()
BadNet.transmit(self.clientSocket,message,self.serverIP,self.serverPort)
lock1.release()
sent+=1
self.clientSocket.close()
def receiveAck(self,lock1,lock2):
print "\n Entered ack !"
lock1.acquire()
ack, serverAddress = self.clientSocket.recvfrom(self.buf)
lock2.release()
if __name__ == "__main__":
lock1 = Lock()
lock2 = Lock()
client = Client();
client.readFile();
thread1 = Thread(target = client.sendFile, args=[lock1,lock2])
thread2 = Thread(target = client.receiveAck, args=[lock1,lock2])
thread1.start()
thread2.start()
thread1.join()
thread2.join()