Created: 2017-06-21 Wed 14:09
Python code to acheive this:
from Threading import Lock lock = Lock() def thread(lock, ): lock.acquire() # code lock.release()
Deadlock occurs when a shared resource is needed by a thread to continue, but is being held by another.
Here's some sample Python code for condition variables (no you can't just copy and paste this and have it work).
# declare a condition variable: cv = threading.Condition() cv.acquire() # Obtain the condition when available, or block. do_the_codes() cv.notifyAll() # tell all waiting threads the condition is free cv.release() # free the condition
You need some kind of normal variable that the condition can check to see if it is it's turn, or the turn of another thread testing the condition.
cv.acquire() if (access==2): print "\nI can do a thing.\n" secs = random.randint(1, 4) time.sleep(secs) cv.notifyAll() cv.release() access=3 else: cv.notifyAll() cv.release()